I need to register the receiver. I used the following template:
@Override protected void onResume() { super.onResume(); registerReceiver(myReceiver, new IntentFilter(...)); } @Override protected void onPause() { super.onPause(); unregisterReceiver(myReceiver); } private BroadcastReceiver myReceiver = new BroadcastReceiver() { ... });
I get crash reports from the market about my unregisterReceiver () call:
java.lang.IllegalArgumentException: Receiver not registered
I thought this was not possible, but it looks like this is the correct template:
private Intent mIntent; @Override protected void onResume() { super.onResume(); if (mIntent == null) { mIntent = registerReceiver(myReceiver, new IntentFilter(...)); } } @Override protected void onPause() { super.onPause(); if (mIntent != null) { unregisterReceiver(myReceiver); mIntent = null; } } private BroadcastReceiver myReceiver = new BroadcastReceiver() { ... });
Is the above the correct pattern? I assume that registration may fail, and should we save the result from registerReceiver () and check it in onPause () before making a call to unregister ()?
thanks
I base a change on this question: Problem with BroadcastReceiver (Recipient registration error)
I saw only the first template above, nowhere where you check the answer for intent - any refinement will be wonderful.
user291701
source share