Android - 确定短信发送失败的原因

时间:2012-06-03 11:28:59

标签: android sms

我有一个提醒应用程序,它将发送短信通知用户他们的提醒时间已经过去。这很好用。但是,我一直在测试当手机睡着并错过提醒时会发生什么。

我正在使用AlarmManager来设置与提醒时间相对应的闹钟。我的记录显示,当手机重新启动并且发送短信的尝试发生时,闹钟响起,但短信从未收到。

所以问题是,有没有办法调试未发送短信的原因?

我的当前代码在发生警报事件时设置ContentObserver:

    private void registerToListenForSentSMS()
{
    MessageSentListener smsObeserver = new MessageSentListener(new Handler());
    ContentResolver contentResolver = TheEveryOtherAlarmAppApplication.getAppContext().getContentResolver();
    contentResolver.registerContentObserver(Uri.parse("content://sms"), true, smsObeserver);
}

MessageSentListener收到发生了一些 SMS事件的通知:

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);
}
}

但这可能是由与我的应用程序无关的SMS事件引起的。无论如何,它并没有让我更接近一个解决方案 - 我想知道什么发生时出了什么问题!

如果没有发送警报短信,我可以重新发送或使用其他一些通知方法,如电子邮件等。

1 个答案:

答案 0 :(得分:5)

您需要广播接收器和未决意图才能收到有关失败短信的通知。

下面的代码段会帮助您

//---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);