这是我在ActivityOne中发送短信的代码
private void sendSMS(final String phoneNumber, final String message,final String userMsg)
{
String SENT = "SMS_SENT";
//String DELIVERED = "SMS_DELIVERED";
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:
Toast toast = Toast.makeText(getApplicationContext(), userMsg, Toast.LENGTH_SHORT);
toast.show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getApplicationContext(), "SMS not sent, Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getApplicationContext(), "SMS not sent, No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getApplicationContext(), "SMS not sent,, Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getApplicationContext(), "SMS not sent, Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, null);
}
我在 Activity 中这样称呼它:
sendSMS(mobileNumber, "My Message", "SMS sent successfully");
我在下一个活动ActivityTwo中有相同的 sendSMS 方法,但是当我从ActivityTwo发送短信时,它每次都会向我显示最后一个活动ActivityOne的祝酒词。
谢谢。