我正在使用聊天应用程序。
在 onPause() 和 onResume() 中,我注册了接收器。
我无法在 onPause() 中取消注册接收器,因为在接收器中我正在检查网络连接
在 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;
}
}
};