使用挂起的意图等待消息的状态(一般错误/无线电关闭/空 PDU/SMS 已发送/SMS 未发送)。
SmsManager smsManager=SmsManager.getDefault();
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
public void sendSms(String phoneNumber,String sms){
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
// do what you need to do if OK
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
// do what you need to do if GENERIC FAILURE
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
// do what you need to do if no Service
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
// Send next sms when previous one delivered your choice
break;
case Activity.RESULT_CANCELED:
//Do something if not delivered
break;
}
}
}, new IntentFilter(DELIVERED));
smsManager.sendTextMessage(phoneNumber, null, sms, sentPI, deliveredPI);
}