当使用unregisterReceiver的onPause()的onDestroy()或的onStop()? [英] When to use unregisterReceiver onPause() , onDestroy() or onStop()?
问题描述
在使用unregisterReceiver 的onPause()
,的onDestroy()
或的onStop()
?
注:我需要的服务在后台运行
。更新:
1.我得到一个异常释放接收器空
。
2.Activity渗漏意图接收器是否缺少调用 unresgisterReceiver();
请告诉我,如果有什么问题
下面是我的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()+释放接收器 - + e.getMessage());
}
}
这取决于您拥有注册接收器。互补的方法对是
的onCreate - 的onDestroy
onResume - 的onPause
ONSTART - 的onStop
如果您注册了第一个接收器,然后注销就在它的结束方式。
When to use unregisterReceiver onPause()
, onDestroy()
or onStop()
?
Note:I need the service to run in background.
Update:
1.I get an exception releasing receivers null
.
2.Activity has leaked intent receivers are you missing call to unresgisterReceiver();
pls 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.
这篇关于当使用unregisterReceiver的onPause()的onDestroy()或的onStop()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!