如何判断android 短信发送(sendTextMessage)是否成功

//短信发送API说明

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent);	
	/**
     * 参数说明
     * destinationAddress:收信人的手机号码
     * scAddress:发信人的手机号码 
     * text:发送信息的内容 
     * sentIntent:发送是否成功的回执,用于监听短信是否发送成功。
     * DeliveryIntent:接收是否成功的回执,用于监听短信对方是否接收成功。
     */

//短信群发

for (int i = 0; i < contactList.size(); i++) {
                Intent itSend = new Intent(SENT_SMS_ACTION);
                itSend.putExtra(KEY_PHONENUM, contactList.get(i));
                PendingIntent mSendPI = PendingIntent.getBroadcast(getApplicationContext(), i/××requestCode××/, itSend, PendingIntent.FLAG_ONE_SHOT/××flag××/);//这里requestCode和flag的设置很重要,影响数据KEY_PHONENUM的传递。
				String content = mContext.getString(R.string.test);
				smsManager.sendTextMessage(contactList.get(i), null, content, mSendPI, null);
			}


public static final String SENT_SMS_ACTION = "SENT_SMS_ACTION";
private SMSSendResultReceiver mSMSReceiver = new SMSSendResultReceiver();
private IntentFilter mSMSResultFilter = new IntentFilter();


//广播注册

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSMSFilter.addAction(SENT_SMS_ACTION);
		registerReceiver(mSMSReceiver, mSMSResultFilter);
		...
	}

//广播定义


class SMSSendResultReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String phoneNum = intent.getStringExtra(KEY_PHONENUM);
            // TODO Auto-generated method stub
                switch(getResultCode())
                {
                    case Activity.RESULT_OK:
                        System.out.println("Send Message to "+phoneNum+" success!");
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                    default:
                        System.err.println("Send Message to "+phoneNum+" fail!");
                        break;
                }
        }
    }










  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值