如何注销与LocalBroadcastManager相关的接收器?
我尝试了以下代码:
我尝试了以下代码:
unregisterReceiver(broadcastReceiver);
但是它给我抛出了一个异常
java.lang.IllegalArgumentException: 未注册的接收器:
这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiverCancelPd, new IntentFilter(PelicanApplication.identifierCancelPd));
LocalBroadcastManager.getInstance(this).registerReceiver(bReceiverSignOut, new IntentFilter(PelicanApplication.identifierSignOut));
LocalBroadcastManager.getInstance(this).registerReceiver(bReceiverNearByDeliveries, new IntentFilter(PelicanApplication.identifierNBD));
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(broadcastReceiverCancelPd);
unregisterReceiver(bReceiverSignOut);
unregisterReceiver(bReceiverNearByDeliveries);
}
LocalBroadcastManager
已被弃用,你知道使用LiveData
或其他方式替代unregisterReceiver()
的方法吗? - C.F.GremoveObserver()
可能是类似的方法,但你可能不需要担心它。也可以看一下observe()
的描述。 - Mike M.