有序广播
无序广播请链接https://blog.youkuaiyun.com/SageDeceiveFiend/article/details/90297374
粘性广播
有序广播
abortBroadcast() 终止有序广播的方法 在重写的onReceive的方法里面写
//清单文件 intent-filter设置广播优先级状态 0-1000 优先级越大 越会先收到广播
<receiver android:name=".MyReceiver">
<intent-filter android:priority="500">
<action android:name="com.wuyanzu"/>
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver1">
<intent-filter android:priority="1000">
<action android:name="com.wuyanzu"/>
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver2">
<intent-filter android:priority="100">
<action android:name="com.wuyanzu"/>
</intent-filter>
</receiver>
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("com.wuyanzu")){
String name = intent.getStringExtra("msg");
Log.e("###", "onReceive: "+name);
}
}
}
public class MyReceiver1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("com.wuyanzu")){
String name = intent.getStringExtra("msg");
Log.e("###", "onReceive1: "+name);
}
}
}
public class MyReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("com.wuyanzu")){
String name = intent.getStringExtra("msg");
Log.e("###", "onReceive2: "+name);
}
}
}
public void dianji(View view) {
Intent intent=new Intent();
intent.setAction("com.wuyanzu");
Bundle bundle=new Bundle();
bundle.putString("msg","我是吴彦祖");
intent.putExtras(bundle);
sendOrderedBroadcast(intent,null);
sendBroadcast(intent);
}
输出效果根据优先级分类
粘性广播
// An highlighted block
public void dianji() {
Intent intent=new Intent();
intent.setAction("com.wuyanzu");
Bundle bundle=new Bundle();
bundle.putString("msg","我是吴彦祖");
intent.putExtras(bundle);
//主要是这个stickybroadcast
sendStickyBroadcast(intent);
}
//线程延迟进行跳转就可以先不执行注册方实现延迟收广播的效果
public void yanchi(){
new Thread(
new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
).start();
}