Android - java.lang.IllegalArgumentException: Receiver not r

时间:2021-01-30 18:24:19

java.lang.IllegalArgumentException: Receiver not registered

从平台上可以看到Crash的错误问题,

出现了以下这条

java.lang.IllegalArgumentException: Receiver not registered

明明我OnResume有注册、OnPause有移除。

但为什么会发生这个问题

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        ... doSomeThing
    }
};

BroadcastReceiver这样命

onResume如下

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(mReceiver, new IntentFilter("filter_action"));
}

onPause如下

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(mReceiver);
}

但会出现如下的错误消息,虽然很少发生,但还是防呆一下(朝向零bug前进)

依照以下这篇文章

http://*.com/questions/4978167/weird-receiver-not-registered-exception

可能防呆改成如下

@Override
protected void onPause() {
    super.onPause();

    try {
        if (mReceiver!= null) {
            unregisterReceiver(mReceiver);
        }
    }catch (Exception ex){
        ex.printStackTrace();
    }
}

原文:大专栏  Android - java.lang.IllegalArgumentException: Receiver not registered