广播作用以及机制
其实BroadCastReceiver就是应用程序间的全局大喇叭,即通信的一个手段, 系统自己在很多时候都会发送广播
项目中的广播
BroadCastReceiver广播接受者,安卓四大组件之一
广播三要素
(1)广播发送者
(2)广播接收者
(3)处理广播的相关信息
广播的静态注册
<!--我是一个广播-->
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.feng.broad"></action>
</intent-filter>
</receiver>
<!--广播结束-->
广播的分类
无序广播
Intent intent = new Intent();
intent.setAction("com.feng.broad");
Bundle bundle = new Bundle();
bundle.putInt("msg",123);
intent.putExtras(bundle);
sendBroadcast(intent);
有序广播
Intent intent1 = new Intent();
intent1.setAction("com.feng.broad");
//第一个参数是intent 二是权限名.
sendOrderedBroadcast(intent1,null);
动态注册广播
myReceiver = new MyReceiver();
//添加广播过滤器
IntentFilter intentFilter = new IntentFilter();
//添加action
intentFilter.addAction(BroadcastConst.ACTION);
//注册
registerReceiver(myReceiver,intentFilter);
系统广播
安卓常用系统广播
https://blog.csdn.net/cc_want/article/details/82344899
接受系统广播
系统在某些时候会发送相应的系统广播,下面我们就来让我们的APP接收系统广播, 接收之前,还需要为我们的APP注册广播接收器哦!而注册的方法又分为以下两种:动态与静态!