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

Broadcast Receiver is called multiple times after registering broadcast receiver

我正在开发 android 可以发送 SMS 的应用程序。发送第一条 SMS 时,广播接收器的 onReceive 方法根据收到的响应将 SMS 的状态显示为 SMS SentSMS DelieveredGeneric Failure。但是当再次发送 SMS 时,onReceiver 首先显示先前发送的消息的状态,然后显示新消息。意味着每次调用 onReceiver 时,它都会显示每条先前发送的消息的状态。下面的class是扩展Worker写的,所以activity像onStoponResume这样的生命周期方法不能在这里被覆盖。

感谢任何帮助。代码如下。

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

你在这里做了很多错误的事情。首先 Broadcast Receiver 不应该在 Worker Thread 中被调用。它始终适用于 UI Thread。因此,每当您创建工作请求时,Broadcast Receiver 都会被注册,因此将在创建工作请求时注册多次。而且您不会在任何地方注销它。 所以会被执行多次。 您应该在 onCreate 中注册广播接收器并在 onStop/onResume 中注销它。这样他们就不会被重复注册了。

希望对您有所帮助 ;).