问题
I have an reminder app which will send an SMS to notifiy the user that their reminder time has passed. This works well. However, I've been testing what happens when the phone has been asleep and missed an reminder.
I'm using AlarmManager to set up alarms to correspond to reminder times. My logging shows that the alarm went off when the phone reboots and an attempt to send an SMS happens, but the SMS is never received.
So the question is, is there a way to debug why an SMS is not sent?
My current code sets up a ContentObserver when an alarm event occurs:
private void registerToListenForSentSMS()
{
MessageSentListener smsObeserver = new MessageSentListener(new Handler());
ContentResolver contentResolver = TheEveryOtherAlarmAppApplication.getAppContext().getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms"), true, smsObeserver);
}
MessageSentListener gets notified that some SMS event happened:
public class MessageSentListener extends ContentObserver
{
public MessageSentListener(Handler handler)
{
super(handler);
}
@Override
public void onChange(boolean selfChange)
{
super.onChange(selfChange);
Log.d(Constants.ALARM_APP_LOG_TAG, "Something happend");
ContentResolver contentResolver = AlarmAppApplication.getAppContext().getContentResolver();
contentResolver.unregisterContentObserver(this);
}
}
But this could be caused by SMS events unrelated to my app. Anyway, it doesn't really get me closer to a solution - I would like to know what went wrong when nothing happens!
In the event that an alarm SMS is not sent, I would be able to either resend or use some other notification method such as email etc.
回答1:
You will need broadcast receiver and pending intent to get notified about failed sms.
Below code snippet will help you
//---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{
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 context, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(context, "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(context, "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(context, "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(context, "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(context, "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(context, "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(context, "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
来源:https://stackoverflow.com/questions/10869933/android-determining-why-sms-sending-failed