android四大组件之BroadcastReceiver

107 阅读2分钟

配置了以上信息之后,只要是android.intent.action.MY_BROADCAST这个地址的广播,MyReceiver都能够接收的到。注意,这种方式的注册是常驻型的,也就是说当应用关闭后,如果有广播信息传来,MyReceiver也会被系统调用而自动运行。

动态注册



MyBoradcastReceiver receiver = new MyBoradcastReceiver ();           

IntentFilter filter = new IntentFilter();  

filter.addAction("android.intent.action.MYBROADCAST_RECEIVER");        

registerReceiver(receiver, filter);  

注意,registerReceiver是android.content.ContextWrapper类中的方法,Activity和Service都继承了ContextWrapper,所以可以直接调用。在实际应用中,我们在Activity或Service中注册了一个BroadcastReceiver,当这个Activity或Service被销毁时如果没有解除注册,系统会报一个异常,提示我们是否忘记解除注册了。所以,记得在特定的地方执行解除注册操作:


@Override  

protected void onDestroy() {  

    super.onDestroy();  

    unregisterReceiver(receiver);  

}  

发送广播

====


public void send(View view) {  

    Intent intent = new Intent("android.intent.action.MYBROADCAST_RECEIVER");  

    intent.putExtra("msg", "hello yummylau!");  

    sendBroadcast(intent);  

} 

广播的种类

=====

普通广播(Normal Broadcast)


普通广播对于多个接收者来说是完全异步的,通常每个接收者都无需等待即可以接收到广播,接收者相互之间不会有影响。对于这种广播,接收者无法终止广播,即无法阻止其他接收者的接收动作。

有序广播(Ordered Broadcast)


有序广播比较特殊,它每次只发送到优先级较高的接收者那里,然后由优先级高的接受者再传播到优先级低的接收者那里,优先级高的接收者有能力终止这个广播。

设置三个广播接受类,通过设置优先级来决定谁先消费该广播


<receiver android:name=".FirstReceiver">  

    <intent-filter android:priority="1000">  

        <action android:name="android.intent.action.MYBROADCAST_RECEIVER"/>  

        <category android:name="android.intent.category.DEFAULT" />  

    </intent-filter>  

</receiver>  

<receiver android:name=".SecondReceiver">  

    <intent-filter android:priority="999">  

        <action android:name="android.intent.action.MYBROADCAST_RECEIVER"/>  

        <category android:name="android.intent.category.DEFAULT" />  

    </intent-filter>  

</receiver>  

<receiver android:name=".ThirdReceiver">  

    <intent-filter android:priority="998">  

        <action android:name="android.intent.action.MYBROADCAST_RECEIVER"/>  

        <category android:name="android.intent.category.DEFAULT" />  

    </intent-filter>  

</receiver>  

三个接收者的多了一个android:priority属性,并且依次减小。这个属性的范围在-1000到1000,数值越大,优先级越高。

发送有序广播


public void send(View view) {  

    Intent intent = new Intent("android.intent.action.MYBROADCAST_RECEIVER");  

    intent.putExtra("msg", "hello yummylau!");  

    sendOrderedBroadcast(intent, "scott.permission.MY_BROADCAST_PERMISSION");  

} 

注意,使用sendOrderedBroadcast方法发送有序广播时,需要一个权限参数,如果为null则表示不要求接收者声明指定的权限,如果不为null,则表示接收者若要接收此广播,需声明指定权限。这样做是从安全角度考虑的,例如系统的短信就是有序广播的形式,一个应用可能是具有拦截垃圾短信的功能,当短信到来时它可以先接受到短信广播,必要时终止广播传递,这样的软件就必须声明接收短信的权限。

所以我们在AndroidMainfest.xml中定义一个权限:


<permission android:protectionLevel="normal"  

            android:name="scott.permission.MYBROADCAST_RECEIVER_PERMISSION" /> 

然后声明这个权限```


如果第一个广播需要添加消息给第二个广播,可以在onReceive()中添加```

Bundle bundle = new Bundle();

bundle.putString("addString","我是添加的消息");

setResultExtras(bundle);

而第二个广播类中取出


Bundle bundle = getResultExtras(true);

String add = bundle.getString("addString");

取消继续传递广播


abortBroadcast();  

本文转自 blog.csdn.net/u010794180/…,如有侵权,请联系删除。