LocalBroadcastManager使用说明

LocalBroadcastManager是Android Support V4包提供了一个局部广播工具,是用来在同一个应用内的不同组件间发送Broadcast的。

LocalBroadcastManager优势如下:

  • 发送的广播只会在自己App内传播,不会泄露给其他App,确保隐私数据不会泄露
  • LocalBroadcastManager  发送的广播不会离开你的应用程序,同样也不会接收来自其它应用程序的广播
  • 比系统全局广播高效
	// 局部广播
	LocalBroadcastManager mLocalBroadcastManager;
	// 广播
	BroadcastReceiver mReceiver;
注册
		mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
		IntentFilter filter = new IntentFilter();
		filter.addAction(ACTION_1);
		filter.addAction(ACTION_2);
		filter.addAction(ACTION_3);
		mReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				if (intent.getAction().equals(ACTION_1)) {
				} else if (intent.getAction().equals(ACTION_2)) {
				} else if (intent.getAction().equals(ACTION_3)) {
				}
			}
		};
		mLocalBroadcastManager.registerReceiver(mReceiver, filter);

取消注册
mLocalBroadcastManager.unregisterReceiver(mReceiver);
发送广播
 mLocalBroadcastManager.sendBroadcast(new Intent(ACTION_1));




你可能感兴趣的:(局部广播,android)