简体   繁体   中英

Broadcast Receiver is called multiple times after registering broadcast receiver

I'm developing android application which is suppose to send SMS. When first SMS is sent, the onReceive method of Broadcast Receiver shows the status of SMS as SMS Sent and SMS Delievered or Generic Failure depending upon the response received. But when again SMS is sent then onReceiver first shows the status of previous sent messsage and then newer message. Means everytime onReceiver is called it shows the status of every previous sent message. The following class is written by extending Worker so activity life cycle methods like onStop and onResume can't be overridden here.

Any help is appreciated. Code is given below.

PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0,
            new Intent(SENT), PendingIntent.FLAG_ONE_SHOT);

PendingIntent deliveredPI = PendingIntent.getBroadcast(getApplicationContext(), 0,
            new Intent(DELIVERED), PendingIntent.FLAG_ONE_SHOT);


 
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    status = sms_id + " : SMS Sent";
                    Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    status = sms_id + " : Generic failure ";
                    Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    status = sms_id + " : No service ";
                    Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    status = sms_id + " : Null PDU  ";
                    Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    status = sms_id + " : Radio off ";
                    Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    IntentFilter filter = new IntentFilter(SENT);
    getApplicationContext().registerReceiver(broadcastReceiver, filter);
    //---when the SMS has been delivered---
    BroadcastReceiver broadcastReceiver1 = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    receiverStatus = sms_id + " : SMS delivered ";
                    Toast.makeText(context, receiverStatus, Toast.LENGTH_SHORT).show();
                    addItem(receiverStatus);
                    break;
                case Activity.RESULT_CANCELED:
                    receiverStatus = sms_id + " : SMS not delivered ";
                    Toast.makeText(context, receiverStatus, Toast.LENGTH_SHORT).show();
                    addItem(receiverStatus);
                    break;
            }
        }
    };
    IntentFilter filter1 = new IntentFilter(DELIVERED);
    getApplicationContext().registerReceiver(broadcastReceiver1, filter1);
    try {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
        handler.post(() -> Toast.makeText(getApplicationContext(), " Do Work", Toast.LENGTH_SHORT).show());
        return Result.success();
    } catch (Exception e) {
        handler.post(() -> Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show());
        return Result.retry();
    }

You are doing plenty of wrong things here. First of all Broadcast Receiver should never be called inside Worker Thread . It always work on UI Thread . So whenever you will create a Work Request Broadcast Receiver is registered and hence will be registered as many times as work request is created. And you are not unregistering it any where. So it will be executed multiple times. You should register Broadcast Receiver inside onCreate and unregister it in onStop/onResume . So that they would not be registered twice.

Hope it Helps ;).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM