broadcast receiver stops when I click back button

北战南征 提交于 2019-12-12 04:45:44

问题


I am working on an Android sms application.The following code I used to send 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);

}

It is working fine when I keep the screen until sms sent. It will write that sms to content/sms/sent or queued depends on sent report.But if i press back button while sending sms .it will send the sms but do not write to content/sms/sent or queued also won,t unregister broadcast too.

Please help me to solve my issue.


回答1:


When a back is pressed, you are basically exiting your application. So inorder to control the behavior when back is pressed, override onBackPressed(). Here you can ensure that content is written, plus you need to unregister broadcast here.




回答2:


I had solved my problem by using service. Using startservice() and stopservice() I can manage registeration.Thanks for the suggestions friends. @Vikki...Your answer gave me the key.



来源:https://stackoverflow.com/questions/12577036/broadcast-receiver-stops-when-i-click-back-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!