AN开发范例实战宝典学习日记二: 短息发送的广播

核心代码:pendingIntent 的使用

1、发送短信:


Intent itSend= new Intent(SMS_SEND_ACTION);

Intent itDeliver= new Intent(SMS_DELIVER_ACTION);

PendingIntent mSendPI = PendingIntent.getBroadcast(getApplicationContent(),0,itSend,0);

PendingIntent mDeliverPI = PendingIntent.getBroadcast(getApplicationContent(),0,itDeliver,0);

SmsManager smsMagager = SmsManager .getDefault();

smsMagager .sendTextMessage(strDestAddress,null,strMessage,mSendPI ,mDeliverPI );


理解:定义一个成功发出短信,一个成功送达短信的Intent,然后使用getBroadcast分别实例化两个 PendingIntent ,

再使用sendTextMessage发送短信,这个方法会把两个Intent广播出去,这个是核心。这样就可以自定义BroadcastReceiver的一个类来实现获取到

上面的两个Intent的业务逻辑


2、代码注册广播:

IntentFilter mFilter1,mFilter2;

mFilter1= new IntentFilter(SMS_SEND_ACTION);

SendReceiver sendReceiver = new SendReceiver();//注册

registerReciever(sendReceiver , mFilter1);


mFilter2= new IntentFilter(SMS_DELIVERED_ACTION);

SendReceiver deliverReceiver = new SendReceiver();

registerReciever(deliverReceiver , mFilter2);


3、SendReceiver类:

public class SendReceiver extends BroadcastReceiver{

   private Context context;

  @Override 

  public void onReceive(Context context,Intent intent){

      this.context=context;

      if(intent.getAction().equals(MainActivity.SMS_SEND_ACTION)){

switch(getResultCode()){

case MainActivity.RESULT_OK:******************;break;

case others:*************************;break;

}

else if(intent.getAction().equals(MainActivity.SMS_DELIVERED_ACTION))){

switch(getResultCode()){

case MainActivity.RESULT_OK:******************;break;

case others:*************************;break;



}

}

   }

}

4、sendTextMessage API

public void sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
destinationAddress 发送短信的地址(也就是号码)
scAddress 短信服务中心,如果为null,就是用当前默认的短信服务中心
text 短信内容
sentIntent 如果不为null,当短信发送成功或者失败时,这个PendingIntent会被广播出去成功的结果代码是Activity.RESULT_OK,或者下面这些错误之一  :RESULT_ERROR_GENERIC_FAILURE
RESULT_ERROR_RADIO_OFF
RESULT_ERROR_NULL_PDU
对于 RESULT_ERROR_GENERIC_FAILURE,  the这个sentIntent可能包括额外的"errorCode",包含一些具体有用的信息帮助检查 。基于SMS控制的全部程序检查 sentIntent. 如果 sentIntent 为空,the caller will be checked against all unknown applications, which cause smaller number of SMS to be sent in checking period.
deliveryIntent 如果不为null,当这个短信发送到接收者那里,这个PendtingIntent会被广播,状态报告生成的pdu(指对等层次之间传递的数据单位)会拓展到数据("pdu")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值