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

时间:2025-01-29 07:39:33
//短信发送API说明

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

//短信群发

for (int i = 0; i < (); i++) {
                Intent itSend = new Intent(SENT_SMS_ACTION);
                (KEY_PHONENUM, (i));
                PendingIntent mSendPI = (getApplicationContext(), i/××requestCode××/, itSend, PendingIntent.FLAG_ONE_SHOT/××flag××/);//这里requestCode和flag的设置很重要,影响数据KEY_PHONENUM的传递。
				String content = ();
				((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) {
		(savedInstanceState);
		(SENT_SMS_ACTION);
		registerReceiver(mSMSReceiver, mSMSResultFilter);
		...
	}

//广播定义


class SMSSendResultReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String phoneNum = (KEY_PHONENUM);
            // TODO Auto-generated method stub
                switch(getResultCode())
                {
                    case Activity.RESULT_OK:
                        ("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:
                        ("Send Message to "+phoneNum+" fail!");
                        break;
                }
        }
    }