【问题标题】:broadcast receiver stops when I click back button当我点击后退按钮时广播接收器停止
【发布时间】:2017-03-26 07:58:06
【问题描述】:

我正在开发一个 Android 短信应用程序。下面是我用来发送短信的代码。

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

}

当我保持屏幕直到发送短信时它工作正常。它会将短信写入内容/短信/已发送或排队取决于发送的报告。但是如果我在发送短信时按下返回按钮。它将发送短信但不写入内容/短信/发送或排队也不会取消注册也广播。

请帮我解决我的问题。

【问题讨论】:

  • 让广播由服务处理
  • 我一定是误读了这个问题,尝试将您的广播接收器设为静态或将其移动到自己的类文件中。
  • @vikki...谢谢...你对这个问题有什么想法吗...stackoverflow.com/questions/11227411/…

标签: android sms android-contentprovider


【解决方案1】:

按下后退时,您基本上是在退出您的应用程序。因此,为了控制按下后退时的行为,请覆盖 onBackPressed()。在这里你可以确保内容被写入,另外你需要在这里取消注册广播。

【讨论】:

  • ...您能否添加更多详细信息.. 示例代码会有所帮助。有时在发送短信后仅按下返回按钮,有时不发送短信,有时在发送时。
【解决方案2】:

我已经通过使用服务解决了我的问题。使用 startservice() 和 stopservice() 我可以管理注册。感谢朋友的建议。 @Vikki...你的回答给了我钥匙。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多