Android 如何在活动中注销接收者?

Android 如何在活动中注销接收者?,android,Android,我正在使用聊天应用程序 在onPause()和onResume()中,我注册了接收者 我无法在onPause()中注销接收器,因为在接收器中我正在检查网络连接 在onPause和onResume中,我必须监视网络 那么我很困惑,在哪里注销接收人 谁能帮帮我吗 protected void onPause(){ backfacekey=1; //unregisterReceiver(mConnReceiver); super.onPause()

我正在使用聊天应用程序

在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;

                        }
                     }
                    };

您可以在
onStop()
onDestroy()
方法中注销接收器。

当活动处于暂停状态时,为什么要检查事件?您还可以在onStop()方法中取消注册接收器。为什么不在Resume上检查网络连接,而不是让接收器处于“后台”状态?解决方案对您有效吗?