I don't understand why I'm getting this exception when hitting the back button. I have the IntentReceiver registered in the onCreate method and it is supposed to be unregistered in the onPause method. My Log.w() call inside of the onPause method leads me to believe that the unregisterReceiver() method is being called, but I am getting this exception still.
Any thoughts?
private PlayerReceiver playerReceiver;
public void onCreate(Bundle savedInsta开发者_Go百科nceState) {
...
IntentFilter playerFilter;
playerReceiver = new PlayerReceiver();
playerFilter = new IntentFilter(PlayerService.BUFFERING_FAILURE);
playerFilter.addAction(PlayerService.BUFFERING_SUCCESS);
registerReceiver(playerReceiver, playerFilter);
...
}
protected void onPause() {
...
if (playerReceiver != null){
unregisterReceiver(playerReceiver);
Log.w(TAG, "playerReceiver has been unregistered");
playerReceiver = null;
}
...
}
public class PlayerReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(PlayerService.BUFFERING_FAILURE)){
setListenButton(false);
}
closePlayDialog();
}
}
LogCat Output
08-26 11:44:28.646: WARN/WWOZMain(1058): playerReceiver has been unregistered
08-26 11:44:29.476: ERROR/ActivityThread(1058): Activity org.wwoz.WWOZMain has leaked IntentReceiver org.wwoz.WWOZMain$PlayerReceiver@43e4dd60 that was originally registered here. Are you missing a call to unregisterReceiver()?
08-26 11:44:29.476: ERROR/ActivityThread(1058): android.app.IntentReceiverLeaked: Activity org.wwoz.WWOZMain has leaked IntentReceiver org.wwoz.WWOZMain$PlayerReceiver@43e4dd60 that was original
ly registered here. Are you missing a call to unregisterReceiver()?
This is a Android activity life cycle issue. I am seeing it in a main activity and then testing on device with the back button that goes back to a splash screen.
In the onPause()
method.
Unregister the BroadcastReceiver
that you created in the onCreate()
In the onRestart()
re-register a brand new Broadcast Receiver.
In the activity class you need to keep a record of the Broadcast Receiver as instance data member.
SECOND
I think this also a feature enhancement issue with Android.
Sometimes developer need a broadcast receiver to outlive the activity. For example to understand when certain screen states are available or not. Think about a conversation context of work flow model, which has many states.
THIRD
You can register and unregister broadcast receivers with an activity, but a simple call like isRegistered(BroadcastReceiver)
in the Activity class might be very useful.
If you need receivers to live beyond the activity, then I do not the answer, except to silence the warning, by adding unregister(X)
in the onDestroy()
call. YMMV ;-)
I had the exact same problem. The cause was that I had inadvertently registered the same BroadcastReceiver twice.
My situation was similar with Mullins, I registered a receiver in both a class and its subclass.
For newbies like me, just toggle a breakpoint at your receiver
, then debug your application. If the receiver is something like com.example.main.listener.MyRecevier@421c0100
, while the error log in logcat is different from it, such as com.example.main.listener.MyRecevier@4202fb40
, with characters after "@" different, surely you registered it twice (or more).
(Above likes a comment under Mullins' answer, actually, I post it here because I can't post comments now.)
精彩评论