我有一個這樣的廣播接收機,任何人都可以幫我取消註冊。我不知道我在哪裏註冊它,所以我無法取消註冊。我從網上獲得的這段代碼,請幫助我如何註冊這個廣播接收機
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);
}
是值得閱讀[谷歌Android開發人員指南(https://開頭開發商.android.com/reference/android/support/v4/content/LocalBroadcastManager.html#unregisterReceiver(android.content.BroadcastReceiver))這是你的goto指南,使用它。 – t0mm13b
[Broadcast receiver not unregistering]的可能重複(https://stackoverflow.com/questions/23717933/broadcast-receiver-not-unregistering) – tephyr