Android 何时取消注册 BroadcastReceiver?在 onPause()、onDestroy() 或 onStop() 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21136464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to unregister BroadcastReceiver? In onPause(), onDestroy(), or onStop()?
提问by Muhammad
When should I use unregisterReceiver? In onPause()
, onDestroy()
, or onStop()
?
我什么时候应该使用 unregisterReceiver?在onPause()
, onDestroy()
, 或onStop()
?
Note: I need the service to run in the background.
注意:我需要该服务在后台运行。
Update:
更新:
I get an exception releasing receivers
null
.Activity has leaked intent receivers are you missing call to
unregisterReceiver();
我得到一个异常释放接收器
null
。Activity 已经泄露了 Intent Receiver 你是否错过了调用
unregisterReceiver();
Please tell me if there's something wrong, here's my code:
请告诉我是否有问题,这是我的代码:
private boolean processedObstacleReceiverStarted;
private boolean mainNotificationReceiverStarted;
protected void onResume() {
super.onResume();
try {
registerReceivers();
} catch (Exception e) {
Log.e(MatabbatManager.TAG,
"MAINActivity: could not register receiver for Matanbbat Action "
+ e.getMessage());
}
}
private void registerReceivers() {
if (!mainNotificationReceiverStarted) {
mainNotificationReceiver = new MainNotificationReceiver();
IntentFilter notificationIntent = new IntentFilter();
notificationIntent
.addAction(MatabbatManager.MATABAT_LOCATION_ACTION);
notificationIntent
.addAction(MatabbatManager.MATABAT_New_DATA_RECEIVED);
notificationIntent
.addAction(MatabbatManager.STATUS_NOTIFCATION_ACTION);
registerReceiver(mainNotificationReceiver, notificationIntent);
mainNotificationReceiverStarted = true;
}
if (!processedObstacleReceiverStarted) {
processedObstacleReceiver = new ProcessedObstacleReceiver();
registerReceiver(processedObstacleReceiver, new IntentFilter(
MatabbatManager.MATABAT_ALARM_LOCATION_ACTION));
processedObstacleReceiverStarted = true;
}
}
private void unRegisterReceivers() {
if (mainNotificationReceiverStarted) {
unregisterReceiver(mainNotificationReceiver);
mainNotificationReceiverStarted = false;
}
if (processedObstacleReceiverStarted) {
unregisterReceiver(processedObstacleReceiver);
processedObstacleReceiverStarted = false;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
try {
unRegisterReceivers();
mWakeLock.release();//keep screen on
} catch (Exception e) {
Log.e(MatabbatManager.TAG, getClass() + " Releasing receivers-" + e.getMessage());
}
}
回答by stinepike
it depends on where you have register the receiver. The complementary method pairs are
这取决于您注册接收器的位置。互补的方法对是
onCreate - onDestroy
onResume - onPause
onStart - onStop
if you register the receiver in the first one then unregister it in it's ending method.
如果您在第一个中注册接收器,则在它的结束方法中取消注册。
回答by Evin1_
From the Android documentation:
You should implement onStop() to release activity resources such as a network connection or to unregister broadcast receivers.
您应该实现 onStop() 以释放活动资源,例如网络连接或取消注册广播接收器。
Then, I would follow these pairs (using @StinePike's analogy):
然后,我会遵循这些对(使用@StinePike 的类比):
onResume - onPause
onStart - onStop
Because of the Android Lifecycle, and as @w3bshark mentioned:
由于Android Lifecycle,正如@w3bshark 提到的:
In post-HoneyComb (3.0+) devices, onStop() is the last guaranteed handler.
在后 HoneyComb (3.0+) 设备中, onStop() 是最后一个有保证的处理程序。
回答by Sudhanshu Gaur
It is just as simple as that, if you want to listen for events even when your activity is not visible then call unregister in onStop() (E.g From Activity A you open Activity B but if you want A to still listening for the events).
就这么简单,如果即使您的活动不可见,您也想监听事件,请在 onStop() 中调用 unregister (例如,从活动 A 您打开活动 B,但如果您希望 A 仍在监听事件) .
But when you only want to listen only for events when your activity is visible then in onPause call unregister() (E.g From Activity A you opened Activity B but now you do not want to listen for events in activity A).
但是,当您只想在您的活动可见时仅侦听事件时,则在 onPause 中调用 unregister() (例如,您从活动 A 中打开了活动 B,但现在您不想在活动 A 中侦听事件)。
Hope this helps your problem.
希望这可以帮助您解决问题。
回答by WonderBoy
An broadcast receiver is an invisible component. All it does it respond to some kind of an change via onReceive() callback.
广播接收器是一个不可见的组件。它所做的一切都是通过 onReceive() 回调响应某种更改。
So it makes sense to activate them , only when your activity is in a state to respond or when it is becoming Enabled /active - which is when onResume() is called.
所以激活它们是有意义的,只有当你的活动处于响应状态或当它变为 Enabled /active 时 - 即调用 onResume() 时。
So it is a better practice to register in onResume() - When activity is visible & Enabled and unregister in onStop() when activity is no longer Active.
因此,在 onResume() 中注册是更好的做法 - 当活动可见并启用时,当活动不再处于活动状态时在 onStop() 中取消注册。