静态广播,又称之为常驻广播,
动态广播
唯一的区别就是注册的方式不同,在AndroidManifest.xml注册的话就是全局广播,如果在某一个Avtivity中注册的话就是局部广播【动态注册】
LocalBroadcastManager【本地广播管理,只在当前应用内进行传播】
//发送广播 Intent intent = new Intent("cn.coreqi.update"); //广播的行为 LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
/** * 接受处理广播 */ private class MyBroadcast extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { switch (intent.getAction()){ case "cn.coreqi.update": break; } } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_handler); MyBroadcast myBroadcast = new MyBroadcast(); IntentFilter intentFilter = new IntentFilter(); //意图筛选 //当前页面接受处理那些广播, intentFilter.addAction("cn.coreqi.update"); //在页面中注册广播,*在页面注册广播后,需要在onDestroy生命周期方法接触注册,否则容易引起内存泄漏 LocalBroadcastManager.getInstance(this).registerReceiver(myBroadcast,intentFilter); } @Override protected void onDestroy() { super.onDestroy(); MyBroadcast myBroadcast = new MyBroadcast(); LocalBroadcastManager.getInstance(this).unregisterReceiver(myBroadcast); }