核心代码:pendingIntent 的使用
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的业务逻辑
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);
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;
}
}
}
}
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") |