简体   繁体   English

注册广播接收器后多次调用广播接收器

[英]Broadcast Receiver is called multiple times after registering broadcast receiver

I'm developing android application which is suppose to send SMS.我正在开发应该发送短信的 android 应用程序。 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.发送第一条 SMS onReceive ,Broadcast Receiver 的onReceive方法根据收到的响应将 SMS 的状态显示为SMS SentSMS DelieveredGeneric Failure But when again SMS is sent then onReceiver first shows the status of previous sent messsage and then newer message.但是当再次发送 SMS 时, onReceiver首先显示以前发送的消息的状态,然后是更新的消息。 Means everytime onReceiver is called it shows the status of every previous sent message.意味着每次调用 onReceiver 时,它都会显示之前发送的每条消息的状态。 The following class is written by extending Worker so activity life cycle methods like onStop and onResume can't be overridden here.以下类是通过扩展Worker编写的,因此无法在此处覆盖onStoponResume等活动生命周期方法。

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 .首先,不应在Worker Thread调用Broadcast Receiver It always work on UI Thread .它始终适用于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.因此,每当您创建工作请求时, Broadcast Receiver被注册,因此将注册与创建工作请求一样多的次数。 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 .您应该在onCreate注册 Broadcast Receiver 并在onStop/onResume取消注册。 So that they would not be registered twice.这样他们就不会被注册两次。

Hope it Helps ;).希望能帮助到你 ;)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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