创建两个测试广播接收者类 CustomerBroadReveiver1 和 CustomerBroadReveiver2android
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及自定以一个 广播接收者类,最后执行他