Android后台发送短信 是否发送成功提醒

目标:按下发送按钮,通过后台向某一特定号码发送一条已编辑好内容的短信。以Toast的形式提示短信是否发送成功、对方是否接收成功。


思路:

1.UI设计非常简单,实现一个按钮即可,不作赘述。


2.这里是发送短文本内容的短信,使用sendTextMessage函数实现,需要使用到SmsManager类。该函数完整定义如下:

public void sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent) 

1.destinationAddress : 目标手机号

2.scAddress :短信中心号码,若为空则使用默认值。

3.text :发送的文本内容。

4.sentIntent : 如果不为空,当消息成功发送或失败这个PendingIntent就调用PendingIntent 中的Intent。

5.deliveryIntent :如果不为空,当消息成功传送到接收者这个PendingIntent就调用PendingIntent 中的Intent 。


使用sentIntent 和 deliveryIntent 就可以完成目标中的提醒功能。


具体实现:

1.添加权限:<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

2.添加两个广播类:

2.1  短信是否发送成功:

public class sendBroadcast extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		//判断短信是否发送成功
		switch (getResultCode())
		{
			case Activity.RESULT_OK:
				Toast.makeText(context, "短信发送成功", Toast.LENGTH_LONG).show() ;
				break ;
			default:
				Toast.makeText(context, "短信发送失败", Toast.LENGTH_LONG).show() ;
				break ;
		}
	}
}
2.2 短信是否被接收:

public class receivedBroadcast extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		//表示对方成功接收到短信
		Toast.makeText(context, "对方接收成功", Toast.LENGTH_LONG).show() ;
	}

}
3.对广播类完成注册:

        <receiver android:name="com.example.smsto_background_2.sendBroadcast"  >
            <intent-filter>
                <action android:name="sendmessage" />
            </intent-filter>
        </receiver>
        
        <receiver android:name="com.example.smsto_background_2.receivedBroadcast"  >
     		<intent-filter>
     		    <action android:name="receivemessage" />
     		</intent-filter>       
        </receiver>
4.在按钮监听器实现功能:

        String sentStr = "sendmessage" ;
	String receiveStr = "receivemessage" ;
	SmsManager smsMgr = SmsManager.getDefault() ;
	Intent sent_intent = new Intent(sentStr) ;
	PendingIntent sentIntent = PendingIntent.getBroadcast(MainActivity.this, 0, sent_intent, 0) ;
	//接收报告:发送方发送短信到目标手机后,若对方成功接收,则返回给运营商一个信号,然后运行商再将这个已成功接收的信号返回给发送方
	Intent delivery_intent = new Intent(receiveStr) ;
	PendingIntent deliveryIntent = PendingIntent.getBroadcast(MainActivity.this, 0, delivery_intent, 0) ;		

	String phonenum = "10010" ;
	smsMgr.sendTextMessage(phonenum, null, "here is a test program.", sentIntent, deliveryIntent) ;



至此,完成目标功能。

如果需要发送长文本短信,可以使用sendMultipartTextMessage和divideMessage函数(用于将长文本内容自动分段)实现。这里不作具体介绍了,直接贴上代码:

SmsManager sms = SmsManager.getDefault() ;
String phonenum = "10010" ;
String msg = "here is a test program." ;
//如果短信内容过长,自动分段
ArrayList<String> list = sms.divideMessage(msg) ;
sms.sendMultipartTextMessage(phonenum, null, list, null, null) ;



Android输入手机号发送短信示例,EditText number框中的是电话号码,EditText body框中的是短信内容:   public void onCreate(Bundle savedInstanceState) {//重写的onCreate方法    super.onCreate(savedInstanceState);    setContentView(R.layout.main);//设置当前的用户界面    send = (Button) this.findViewById(R.id.send);//得到发送按钮的引用    number = (EditText) this.findViewById(R.id.number);//得到电话号码文本框的引用    body = (EditText) this.findViewById(R.id.body);//得到短信内容文本框的引用    send.setOnClickListener(this);//添加监听   IntentFilter myIntentFilter = new IntentFilter("SMS_SEND_ACTION");//创建过滤器   MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver();//创建BroadcastReceiver   registerReceiver(myBroadcastReceiver, myIntentFilter);//注册BroadcastReceiver    }   @Override   public void onClick(View v) {//监听方法   if(v == send){//按下发送按钮    send.setEnabled(false);//设置按钮为不可用    String strNumber = number.getText().toString();//得到电话号码    String strBody = body.getText().toString();//得到需要发送的信息    SmsManager smsManager = SmsManager.getDefault();//得到SmsManager    Intent intentSend = new Intent("SMS_SEND_ACTION");//创建Intent    PendingIntent sendPI = PendingIntent.getBroadcast(getApplicationContext(), 0, intentSend, 0);    smsManager.sendTextMessage(strNumber, null, strBody, sendPI, null);//发送短信    send.setEnabled(true);//设置按钮为可用   }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值