Android - java.lang.IllegalArgumentException: Receiver not registered

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://stackoverflow.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();
    }
}