创建本地广播
初始化
private MyReceiver myReceiver;
private LocalBroadcastManager localBroadcastManager;
初始化广播
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.e("xxr", "onReceive: " );
}
}
创建本地广播
localBroadcastManager=LocalBroadcastManager.getInstance(this);
myReceiver=new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("aaaaaaa");
localBroadcastManager.registerReceiver(myReceiver,intentFilter);
发送广播
Intent intent = new Intent();
intent.setAction("aaaaaaa");
localBroadcastManager.sendBroadcast(intent);
广播使用完毕 需要销毁
@Override
protected void onDestroy() {
super.onDestroy();
localBroadcastManager.unregisterReceiver(myReceiver);
}