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

huangapple go评论67阅读模式
英文:

Broadcast Receiver is called multiple times after registering broadcast receiver

问题

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

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

答案1

得分: 2

你在这里做了很多错误的事情。首先,Broadcast Receiver 绝不应该在 Worker Thread 内被调用。它总是在 UI Thread 上运行。因此,无论何时创建工作请求,Broadcast Receiver 都会被注册,因此将根据创建的工作请求次数注册多次。而且你没有在任何地方取消注册它。
因此,它将被多次执行。
你应该在 onCreate 中注册 Broadcast Receiver,并在 onStop/onResume 中取消注册。这样就不会重复注册。

希望能帮到你 广播接收器在注册广播接收器后被多次调用。

英文:

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

huangapple
  • 本文由 发表于 2020年8月31日 17:00:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63667793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定