该组件分为2种
一种是无顺序异步的 只要一发广告 所有能接收到服务的应用都能同时收到。
发送:
sendBroadcast(intent);
接收
public class MyRecieve extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //TODO sth. } }
一种是有序的 通过设置priority的优先级来排序 -1000~1000 利用abortBroadcast()结束传播
发送:
sendOrderedBroadcast(intent, null);
多个广播接收者之间传值:
Bundle bundle=new Bundle(); bundle.putString("a", "fdsfs"); setResultExtras(bundle); //取值 getResultExtras(false).getString("a")
配置:
<receiver android:name=".MySecBroad"> <!--android:priority优先级 --> <intent-filter android:priority="20"> <action android:name="com.mytest.action_MY_BROADCAST"/> </intent-filter> </receiver>
手动注册BroadCastReceiver:
MySecBroad receiver=new MySecbroad(); IntentFilter filter=new IntentFilter(); filter.addAction(TEST_ACTION); registerReceiver(receiver,filter);