1.无序广播,就普通广播,没有先后顺序,几乎同时收到消息
2.有序广播,可以设置接收器的顺序优先级,还可以对广播设置设置截断,修改
通过设置abortBroadcast()进行截断,通过setResultExtras(bundle)向下游广播接收器传递额外的键值对信息或者setResultData(“”)直接传送字符串,下游广播通过getResultExtras方法接收信息,getResultData()方法接收字符串信息
示例有序广播
清单文件:
有序广播的顺序通过清单文件里的android:priority属性的数字大小进行优先级设置:范围为(-1000到1000)数字越大,优先级越高,
可参考下图:
/**
* 有序广播01
*/
public class MyHaveBroadcastReceiver01 extends BroadcastReceiver {
private static final String TAG = "MyHaveBroadcastReceiver01";
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
Bundle extras = intent.getExtras();
String data = "";
if (extras != null) {
data = extras.getString("name");
}
Log.d(TAG, "MyHaveBroadcastReceiver01 onReceive"+data);
}
}
}
/**
* 有序广播02
*/
public class MyHaveBroadcastReceiver02 extends BroadcastReceiver {
private static final String TAG = "MyHaveBroadcastReceiver02";
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
Bundle extras = intent.getExtras();
String data = "";
if (extras != null) {
data = extras.getString("name3");
/**
* 2.1有序广播可以通过setResultExtras向下游广播接收器传递数据
*/
extras.putString("name2", "喊你速速到龙阳路集合");
setResultExtras(extras);
/**
* 2.2有序广播可以通过setResultData向下游广播接收器传递字符串
*/
setResultData("2号口的信息");
}
Log.d(TAG, "MyHaveBroadcastReceiver02 onReceive" + data);
/**
* 1.有序广播可以通过abortBroadcast();方法对广播进行截断
*/
// abortBroadcast();
}
}
}
/**
* 有序广播03
*/
public class MyHaveBroadcastReceiver03 extends BroadcastReceiver {
private static final String TAG = "MyHaveBroadcastReceiver03";
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
Bundle extras = intent.getExtras();
String data = "";
if (extras != null) {
data = extras.getString("name");
}
Log.d(TAG, "MyHaveBroadcastReceiver03 onReceive" + data);
/**
* 获取上游广播传递过来的数据
*/
//方法1
Bundle bundle = getResultExtras(true);
String strData = bundle.getString("name2");
String resultData = getResultData();
//方法2,直接获取字符串
if (strData != null) {
Log.d(TAG, "上游广播传递来的信息 onReceive" + strData +"\t\t\t"+ resultData);
}
}
}
}
Activity页面:StaticDynamicTestActivity.java
/**
* 静态广播和动态广播的测试
* 默认情况下,静态注册的广播隐式的收不到
* 有序广播和无序广播的测试
*/
public class StaticDynamicTestActivity extends AppCompatActivity {
Button btnHaveOrder;
Button btnNoHaveOrder;
private MyDyBroadcastReceiver myDyBroadcastReceiver = new MyDyBroadcastReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_static_dynamic_test);
//注册动态广播接收器
IntentFilter filter = new IntentFilter();
filter.addAction("my_dy_broadcast_receiver");
//注册
registerReceiver(myDyBroadcastReceiver, filter);
registerReceiver(mBatteryBroadcast, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
btnHaveOrder = findViewById(R.id.btn_haveOrder);
btnNoHaveOrder = findViewById(R.id.btn_noOrder);
haveOrder();
noOrder();
}
//发送静态广播
@SuppressWarnings("wrongConstant")
public void sendStaticBroadcast(View view) {
Intent intent = new Intent();
intent.setAction("my_broadcast_receiver");
//安卓8.0以及以后做了禁止后台运行,不加setPackage无法收到隐式广播或者以下
//intent.addFlags(0x01000000);//不推荐
intent.setPackage(getPackageName());
sendBroadcast(intent);
}
//发送动态广播
@SuppressWarnings("wrongConstant")
public void sendDyBroadcast(View view) {
Intent intent = new Intent();
intent.setAction("my_dy_broadcast_receiver");
// //安卓8.0以及以后做了禁止后台运行,不加setPackage无法收到隐式广播或者以下
// //intent.addFlags(0x01000000);//不推荐
// intent.setPackage(getPackageName());
sendBroadcast(intent);
}
BroadcastReceiver mBatteryBroadcast = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra("level", 0);
Toast.makeText(context, "电池电量还有百分之" + level, Toast.LENGTH_LONG).show();
}
};
/**
* 有序广播
*/
void haveOrder() {
btnHaveOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction("my_have_broadcast_receiver");
//这里注意传递个bundle参数
Bundle bundle = new Bundle();
bundle.putString("name3","回家吃饭了");
intent.putExtras(bundle);
//安卓8.0以及以后做了禁止后台运行,不加setPackage无法收到隐式广播或者以下
intent.setPackage(getPackageName());
sendOrderedBroadcast(intent, null);
}
});
}
/**
* 无序广播
*/
void noOrder() {
btnNoHaveOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction("my_no_broadcast_receiver");
//安卓8.0以及以后做了禁止后台运行,不加setPackage无法收到隐式广播或者以下
intent.setPackage(getPackageName());
sendBroadcast(intent);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
//解除注册
unregisterReceiver(myDyBroadcastReceiver);
unregisterReceiver(mBatteryBroadcast);
}
}
Activity对应的xml页面:activity_static_dynamic_test.xml
以上为简单使用参考:有问题加QQ:1393508286