定义广播类
建立两个测试广播接收者类 CustomerBroadReveiver1 和 CustomerBroadReveiver2
package com.tang.customerbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class CustomerBoradcast1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("广播1");
//Toast.makeText(context, "收到广播了", Toast.LENGTH_SHORT).show();
}
}
配置清单 (广播优先级越大越先执行)
<receiver android:name=".CustomerBoradcast1">
<intent-filter android:priority="1">
<action android:name="android.customer.xxoo1"/>
</intent-filter>
</receiver>
<receiver android:name=".CustomerBroadcast2">
<intent-filter android:priority="10">
<action android:name="android.customer.xxoo1"/>
</intent-filter>
</receiver>
测试发送广播代码
Intent it = new Intent();
it.setAction("android.customer.xxoo1");
//sendBroadcast(it); //每个广播接收者都会收到消息
//sendOrderedBroadcast(it, null);//有序发送,广播接收者会按照优先级接受广播事件
//第三个参数可以在最后还能在执行一个广播接收者
sendOrderedBroadcast(it, null, new FinalReceiver(), null, 0, null, null);
#FinalReceiver及自定以一个 广播接收者类,最后执行他