在 onResume() 中销毁 BroadcastReceiver
Destroy BroadcastReceiver in onResume()
我的 MainActivity 中有 onResume() 和 onDestroy() 方法。如何销毁 onResume() 方法中的 registerReceiver。我试图将它分配给变量但没有成功。
@Override
protected void onResume() {
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
check_wifi_available();
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
@Override
protected void onDestroy() {
unregisterReceiver();
super.onDestroy();
}
来自 android 文档:
If registering a receiver in your Activity.onResume() implementation,
you should unregister it in Activity.onPause(). (You won't receive
intents when paused, and this will cut down on unnecessary system
overhead)
将您的接收器定义为 class 变量,如下所示:
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
check_wifi_available();
}
}
然后像这样实现 onPause() / onResume() 方法:
@Override
protected void onResume() {
super.onResume();
registerReceiver(mReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
无需在 onDestroy() 方法中取消注册。
在您的 class' 属性中定义接收器:
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
和这些方法:
@Override
public void onResume() {
super.onResume();
registerReceiver(broadcastReceiver, new IntentFilter("your package name"));
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
}
我的 MainActivity 中有 onResume() 和 onDestroy() 方法。如何销毁 onResume() 方法中的 registerReceiver。我试图将它分配给变量但没有成功。
@Override
protected void onResume() {
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
check_wifi_available();
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
@Override
protected void onDestroy() {
unregisterReceiver();
super.onDestroy();
}
来自 android 文档:
If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead)
将您的接收器定义为 class 变量,如下所示:
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
check_wifi_available();
}
}
然后像这样实现 onPause() / onResume() 方法:
@Override
protected void onResume() {
super.onResume();
registerReceiver(mReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
无需在 onDestroy() 方法中取消注册。
在您的 class' 属性中定义接收器:
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
和这些方法:
@Override
public void onResume() {
super.onResume();
registerReceiver(broadcastReceiver, new IntentFilter("your package name"));
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
}