-1

我有一個這樣的廣播接收機,任何人都可以幫我取消註冊。我不知道我在哪裏註冊它,所以我無法取消註冊。我從網上獲得的這段代碼,請幫助我如何註冊這個廣播接收機

public class MainActivity extends baseActivity { 
    private DrawerLayout drawerLayout; 
    private Toolbar toolbar; 
    Button b1; 
    Animation myAnim; 
    RelativeLayout r; 
    ViewPager viewPager; 
    TextView textView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.content_main); 


     IntentFilter intentFilter = new IntentFilter(NetworkStateChangeReceiver.NETWORK_AVAILABLE_ACTION); 
     BroadcastReceiver broadCastReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       boolean isNetworkAvailable = intent.getBooleanExtra(IS_NETWORK_AVAILABLE, false); 
       String networkStatus = isNetworkAvailable ? "connected" : "disconnected"; 
       Snackbar snack = Snackbar 
         .make(findViewById(R.id.activity_main), "Network Status : " + networkStatus, Snackbar.LENGTH_SHORT); 
       View sbView = snack.getView(); 
       TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text); 
       textView.setTextColor(Color.YELLOW); 
       if (networkStatus=="connected") { 
        snack.show(); 
       } 
       else if(networkStatus=="disconnected"){ 
        snack.make(findViewById(R.id.activity_container), "Network Status : " + networkStatus, Snackbar.LENGTH_SHORT); 
        snack.show(); 
       } 
      } 
     }; 
     LocalBroadcastManager.getInstance(this).unregisterReceiver(broadCastReceiver); 





       //toolbar = (Toolbar) findViewById(R.id.toolbar); 
     myAnim= AnimationUtils.loadAnimation(this, R.anim.bounce); 
     //setSupportActionBar(toolbar); 
     //initNavigationDrawer(); 
     viewPager = (ViewPager) findViewById(R.id.splash); 
     ImageAdapter adapter = new ImageAdapter(this); 
     viewPager.setAdapter(adapter); 

    } 
+0

是值得閱讀[谷歌Android開發人員指南(https://開頭開發商.android.com/reference/android/support/v4/content/LocalBroadcastManager.html#unregisterReceiver(android.content.BroadcastReceiver))這是你的goto指南,使用它。 – t0mm13b

+0

[Broadcast receiver not unregistering]的可能重複(https://stackoverflow.com/questions/23717933/broadcast-receiver-not-unregistering) – tephyr

回答

0

你必須參考保持到您創建的廣播接收器,然後使用unregisterReceiver方法LocalBroadcastManager的

public class MainActivity extends baseActivity { 
private DrawerLayout drawerLayout; 
private Toolbar toolbar; 
Button b1; 
Animation myAnim; 
RelativeLayout r; 
ViewPager viewPager; 
TextView textView; 
private BroadcastReceiver broadCastReceiver; 
private boolean lastState; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.content_main); 
    lastState = false; 
    broadCastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      boolean isNetworkAvailable = intent.getBooleanExtra(IS_NETWORK_AVAILABLE, false); 
      if(lastState == isNetworkAvailable) { 
       return; 
      } 
      lastState = isNetworkAvailable; 
      String networkStatus = isNetworkAvailable ? "connected" : "disconnected"; 
      Snackbar snack = Snackbar 
        .make(findViewById(R.id.activity_main), "Network Status : " + networkStatus, Snackbar.LENGTH_SHORT); 
      View sbView = snack.getView(); 
      TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text); 
      textView.setTextColor(Color.YELLOW); 
      if (networkStatus=="connected") { 
       snack.show(); 
      } 
      else if(networkStatus=="disconnected"){ 
       snack.make(findViewById(R.id.activity_container), "Network Status : " + networkStatus, Snackbar.LENGTH_SHORT); 
       snack.show(); 
      } 
     } 
    }; 





    //toolbar = (Toolbar) findViewById(R.id.toolbar); 
    myAnim= AnimationUtils.loadAnimation(this, R.anim.bounce); 
    //setSupportActionBar(toolbar); 
    //initNavigationDrawer(); 
    viewPager = (ViewPager) findViewById(R.id.splash); 
    ImageAdapter adapter = new ImageAdapter(this); 
    viewPager.setAdapter(adapter); 

} 

@Override 
public void onPause() { 
    super.onPause(); 
    try { 
     unregisterReceiver(broadCastReceiver); 
    } catch (java.lang.IllegalArgumentException exc) { 
    } 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    registerReceiver(broadCastReceiver, new IntentFilter(NetworkStateChangeReceiver.NETWORK_AVAILABLE_ACTION)); 
} 
+0

它不工作我有編輯代碼 – user8076850

+0

你可以在編輯後向我顯示代碼嗎? –

+0

我已編輯它 – user8076850

0

試試看下面的代碼。

public void unregisterBroadcast(){ 
    PackageManager pm = this.getPackageManager(); 
    ComponentName componentName = new ComponentName(NetworkService.this, ConnectivityStateReceiver.class); 
    pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 
      PackageManager.DONT_KILL_APP); 
    Log.e(TAG, "Broadcast: DISABLED!"); 
} 
+0

它在這裏詢問一些參數或表達式public void unregisterBroadcast() – user8076850