2 clcurie 11 clcurie_11 于 2013.05.20 15:58 提问

点击返回按钮时 broadcast receiver 停止

我创建了一个android sms 应用程序。我使用下面的代码来发送sms。

public void sendSms(final String phoneNumber, final 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 --
    sendBroadcastReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent",
                    Toast.LENGTH_SHORT).show();
                    ContentValues values = new ContentValues();
                    values.put("address", phoneNumber);
                    values.put("body", message);
                    getContentResolver().insert(Uri.parse("content://sms/sent"), values);
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure",
                    Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service",
                    Toast.LENGTH_SHORT).show();
                    ContentValues values1 = new ContentValues();
                    values1.put("address", phoneNumber);
                    values1.put("body", message);
                    getContentResolver().insert(Uri.parse("content://sms/queued"), values1);
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU",
                        Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off",
                        Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
            context.unregisterReceiver(this);
        }
    };
    SmsManager sms = SmsManager.getDefault();
        registerReceiver(sendBroadcastReceiver , new IntentFilter(SENT));
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}

当sms发送时屏幕上看都没有问题。根据发送报告sms可以写入content/sms/sent 或者queued中。但是当发送sms时,我点击返回按钮,它可以发送sms但是不能写入content/sms/sent 或者queued中,也不会注销broadcast。
如何处理这个问题呢?

1个回答

baimy1985
baimy1985   2013.05.20 21:16

你的BroadcastReceiver返回退出界面销毁了吧,总感觉你的写法怪怪的。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!