每日日报

有序广播和无序广播

发送无序广播  创建intent 设置action 通过sendBroadcast(intent) 就可以把广播发出去  当前的设备上 只要有广播接收者注册了相同的action 就可以收到广播 并且在发广播的时候 可以通过intent传递数据
 1.public void sendbroadcast(View v){
2.        Intent intent = new Intent();
3.        intent.setAction("com.itheima.broadcast");
4.        intent.putExtra("key", "hello");
5.        sendBroadcast(intent);
6.    }

接收无序广播
注册广播接收者 指定对应的action就可以收到这个广播
 1.<receiver android:name="com.itha.receivercostumbroadcast.CostumReceiver">
2.            <intent-filter >
3.                <action android:name="com.itha.broadcast"/>
4.            </intent-filter>
5.        </receiver>

如何区分有序广播 无序广播
接收到广播之后 在onReceive方法中调用abortbroadcast方法 如果没有异常说明是有序广播
如果 BroadcastReceiver trying to return result during a non-ordered broadcast 有这个异常说明是无序广播
 
  接收的顺序 是否可以中断 发送的方法
有序广播(可以修改广播内容) 可以通过priority 设置接收顺序 abortbroadcast();可以中断 sendOrderedbroadcast
无序广播(不可以修改) 大家一起收到 不可以中断 sendBroadCast()
原文地址:https://www.cnblogs.com/zhukaile/p/14837198.html