`
chenyunhong
  • 浏览: 136106 次
  • 性别: Icon_minigender_1
  • 来自: 真的不知道
社区版块
存档分类
最新评论

Android简单短信发送示例

 
阅读更多

1、首先创建Android项目

Activity代码如下:

package com.cyh.sms;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText number;
	private EditText message;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //获取电话号码
        number = (EditText)this.findViewById(R.id.number);
        //获取短信内容
        message = (EditText)this.findViewById(R.id.message);
        //获取发送按钮
        Button button = (Button)this.findViewById(R.id.button);
        //发送按钮注册点击事件
        button.setOnClickListener(new ButtonClickListener());
    }
    
    //button点击响应事件
    private final class ButtonClickListener implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			String tel = number.getText().toString();
			String sms = message.getText().toString();
			
			SmsManager manager = SmsManager.getDefault();
			//对消息内容进行拆分(因为短信发送字数是有限制的)
			ArrayList<String> smss = manager.divideMessage(sms);
			/**
			 * 第一个参数,目的地址
			 * 第二个参数,短信中心号码,null为使用默认的
			 * 第三个参数,短信内容
			 * 第四个参数,用于得到发送的状态
			 * 第五个参数,得到对方是否收到短信
			 */
			for(String text:smss){
				manager.sendTextMessage(tel, null, text, null, null);
			}
			Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG).show();
		}
    }
}
 

 

strings.xml代码如下:

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">短信发送器</string>
    <string name="number">请输入手机号码</string>
    <string name="message">请输入短信内容</string>
    <string name="button">发送短信</string>
    <string name="success">短信发送成功</string>
</resources>
 

 

main.xml代码如下:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<RelativeLayout  android:layout_width="fill_parent"
    			 android:layout_height="wrap_content">
    			 
<TextView  
    android:layout_width="100px" 
    android:layout_height="wrap_content" 
    android:text="@string/number"
    android:id="@+id/numberlable"
    
    />
    
<EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/number"
    android:layout_toRightOf="@id/numberlable"
    android:layout_alignTop="@id/numberlable"
    android:layout_marginLeft="5px"
    />

</RelativeLayout>   

    
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/message"
    />
    
<EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:minLines="3"
    android:id="@+id/message"
    />
    
<Button  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button"
    android:id="@+id/button"
    /> 
</LinearLayout>

 

 AndroidManifest.xml代码如下:

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.cyh.sms"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="8" />
    <!-- 使用发送短消息权限 -->
	<uses-permission android:name="android.permission.SEND_SMS"/>
</manifest> 
   

 

分享到:
评论

相关推荐

    Android输入手机号发送短信示例.rar

    Android输入手机号发送短信示例,EditText number框中的是电话号码,EditText body框中的是短信内容:  public void onCreate(Bundle savedInstanceState) {//重写的onCreate方法  super.onCreate...

    Android发送接收手机验证码的完整示例.rar

    Android发送接收手机验证码的完整示例,通过短信来验证的方式的例子,比较完整的例子,包括了一个输入手机号码获取验证码并且进行验证的例子,源代码也在里面。本例子涉及到Android发送短信验证码的方法,每隔一分钟...

    云信短信接口示例demo v1.0

    短信接口优势:1、接口支持一对一发送、群发、上行回复、发送状态、查看发送记录、查看余额等2、支持asp、.net、delphi、vb、vc++、java、C#、php等主流开发语言,支持windows、linux、unix、android、ios等多种运行...

    Android实现发送短信验证码倒计时功能示例

    本篇文章主要介绍了Android实现发送短信验证码倒计时功能示例,这里整理了详细的代码,有需要的小伙伴可以参考下。

    Android发送短信方法实例详解

    本文实例讲述了Android发送短信方法。分享给大家供大家参考,具体如下: 短信和打电话一样,都是android手机的基本功能,下面以实例说明android如何实现发送短信的功能。 程序如下所示: import java.util.regex....

    Android开发中实现发送短信的小程序示例

    import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsManager; import android.util.Log; im

    Android编程实现短信收发及语音播报提示功能示例

    主要介绍了Android编程实现短信收发及语音播报提示功能,结合实例形式分析了Android实现短信的接收、发送以及相应的语音播报提示功能相关操作技巧,需要的朋友可以参考下

    Android编程实现定时发短信功能示例

    主要介绍了Android编程实现定时发短信功能,结合实例形式较为详细的分析了Android定时发送短信功能的相关原理、实现方法与注意事项,需要的朋友可以参考下

    利用smsmanager实现后台发送短信示例

    主要介绍了android利用SmsManager可以实现后台发送短信的方法,最近有使用说明,大家可以参考使用

    Android应用源码安卓源码(172个合集).zip

    Android自动发送短信.rar Android自动开关机实现.rar Android视频采集+RTSP完整代码(可用) Android远程登录含有loading登录效.zip Angle v1.0_2D游戏引擎.ZIP BOOK看遍所有UI控件.7z BrewClock闹钟.zip ...

    android 短信 邮件

    (1)通过网络 将尽可能多的短信给指定的手机号的软件,给其他手机号码发送信息。 (2)可以配置SMTP 的方式 给指定的邮箱发送邮件,为了防止用户首次使用不清楚流程,系统内置了 smtp的示例,用于帮助用户快速上手。 ...

    android天气预报项目报告

    示例还需要提供基于SMS短信的天气数据服务,其他手机用户可以向本示例所在的手机上发送SMS短信,在短信中包含特定的关键字,则可以将已有的天气情况通过SMS短信回复给用户。最后,每个被发送的SMS短信都会被记录下来...

    Android中验证码倒计时的简单实现方法示例

    现在的很多app都是使用手机注册的,为了确认使用的是自己的手机,都会加上一个短线验证码的选项,最近公司的项目使用到了这个短信验证码,并且要加入验证码倒计时功能,也就是60秒才能发送一次验证码,再次做过记录...

    Android编程实现的短信编辑器功能示例

    修改短信数据库,从而生成任意手机号发送的短信。 AndroidManifest.xml &lt;?xml version=1.0 encoding=utf-8?&gt; &lt;manifest xmlns:android=http://schemas.android.com/apk/res/android package=...

    二个android模拟器互发短信程序演示

    二个android模拟器互发送短信程序示例,大家参考使用吧

    Advance-Android-Tutorials, 高级Android教程.zip

    Advance-Android-Tutorials, 高级Android教程 在android示例 http://javatechig.com/android/sending-sms-message-in-android/ 中发送短信的费用为 。Android JSON提要阅读器 http://javatech

    android教程之intent的action属性使用示例(intent发短信)

    主要介绍了android中intent的action属性使用示例,提供了使用intent拨打电话、发送短信、播放mp3的代码

Global site tag (gtag.js) - Google Analytics