当注销广播接收器?在的onPause()的onDestroy(),或的onStop()? [英] When to unregister BroadcastReceiver? In onPause(), onDestroy(), or onStop()?
问题描述
当我应该使用unregisterReceiver?在的onPause()
,的onDestroy()
或的onStop()
?
请注意:我需要在后台运行的服务。
。更新:
-
我得到一个异常释放接收器
空
。 -
活动已泄漏的意图接收器是否缺少调用
unregisterReceiver();
请告诉我,如果有什么问题,这是我的code:
私人布尔processedObstacleReceiverStarted;
私人布尔mainNotificationReceiverStarted;保护无效onResume(){ super.onResume();
尝试{
registerReceivers(); }赶上(例外五){ Log.e(MatabbatManager.TAG,
MAINActivity:无法注册接收器Matanbbat行动
+ e.getMessage());
}
}私人无效registerReceivers(){ 如果(!mainNotificationReceiverStarted){
mainNotificationReceiver =新MainNotificationReceiver(); IntentFilter的notificationIntent =新的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; } 如果(!processedObstacleReceiverStarted){
processedObstacleReceiver =新ProcessedObstacleReceiver();
registerReceiver(processedObstacleReceiver,新的IntentFilter(
MatabbatManager.MATABAT_ALARM_LOCATION_ACTION));
processedObstacleReceiverStarted = TRUE; }}私人无效unRegisterReceivers(){ 如果(mainNotificationReceiverStarted){
unregisterReceiver(mainNotificationReceiver);
mainNotificationReceiverStarted = FALSE;
}
如果(processedObstacleReceiverStarted){
unregisterReceiver(processedObstacleReceiver);
processedObstacleReceiverStarted = FALSE;
}}
@覆盖
保护无效的onDestroy(){
// TODO自动生成方法存根
super.onDestroy(); 尝试{ unRegisterReceivers();
mWakeLock.release(); //保持屏幕上
}赶上(例外五){
Log.e(MatabbatManager.TAG,的getClass()+释放receivers-+ e.getMessage());
}}
这取决于你在哪里有注册的接收器。互补的方法对是
的onCreate - 的onDestroy
onResume - 的onPause
在onStart - 的onStop
如果您注册了第一个接收器,然后注销它在它的结束方式。
When should I use unregisterReceiver? In onPause()
, onDestroy()
, or 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();
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());
}
}
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.
这篇关于当注销广播接收器?在的onPause()的onDestroy(),或的onStop()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!