我正在使用聊天应用程序。
在onPause()和onResume()中,我注册了接收器。
我无法在onPause()中取消注册接收器,因为在接收器Iam中检查网络连接
在onPause和onResume中我必须监控网络
然后我很困惑,在哪里取消注册接收器?
有人可以帮助我。
protected void onPause(){
backfacekey=1;
//unregisterReceiver(mConnReceiver);
super.onPause();
registerReceiver(mFacebookReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
protected void onResume(){
backfacekey=0;
super.onResume();
registerReceiver(mFacebookReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
private BroadcastReceiver mFacebookReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo info = (NetworkInfo)intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) {
Toast.makeText(getApplicationContext(), "Network disconnected",Toast.LENGTH_LONG).show();
}
key=2;
}
if (info.getState().equals(NetworkInfo.State.CONNECTED)) {
if(key==2){
Toast.makeText(getApplicationContext(), "Network connected ",Toast.LENGTH_LONG).show();
key=0;
}
}
};
答案 0 :(得分:1)
您可以使用onStop()
或onDestroy()
方法取消注册接收器。