6

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

AlarmManager用来设置警报以对应提醒时间。我的日志显示,当手机重新启动并尝试发送 SMS 时警报响起,但从未收到 SMS。

所以问题是,有没有办法调试为什么不发送短信?

我当前的代码在发生警报事件时设置了 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 事件引起的。无论如何,它并没有真正让我更接近解决方案 - 我想知道什么都没发生时出了什么问题!

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

4

1 回答 1

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);  
于 2012-06-03T12:34:14.877 回答