有序广播、无序广播

来源:互联网 发布:卡qq会员永久软件 编辑:程序博客网 时间:2024/05/16 14:51

BroadcastReceiver所对应的广播分两类:无序广播和有序广播。
无序广播:
通过Context.sendBroadcast()方法来发送。它是完全异步的。所有的receivers接收器的执行顺序不确定。因此,所有的receivers接收器接收broadcast的顺序不确定。这种广播的效率比较高,但意味着无法被截断的。
有序广播:
从优先级别最高的广播接收器开始接收,接收完了如果没有丢弃,就下传给下一个次高优先级别的广播接收器进行处理,依次类推,直到最后。

优先级就是在清单文件中注册广播接受者时定义的android:priority=“ ”参数,优先级的范围是-1000~1000之间。
当两个广播接受者的优先级相同时,则先注册的组件优先接收到广播。
当两个应用程序监听了同一个广播事件并设置了优先级,则先安装的应用优先接收到广播。

下面我们就通过一个小案例演示一下:

布局如图所示:
这里写图片描述

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.thinkpad.myapplication.MainActivity"    android:background="@drawable/stitch_one">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_marginTop="80dp"        android:onClick="send"        android:text="发送有序广播"        android:textSize="20sp"        android:paddingRight="5dp"        android:paddingLeft="5dp"        android:background="#FBFBFF"/></RelativeLayout>

MainActivity:

package com.example.thinkpad.myapplication;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    public void send (View view){        Intent intent =new Intent();        intent.setAction("Intercept_Stitch");        sendOrderedBroadcast(intent,null);    }}

MyReceiverOne:

package com.example.thinkpad.myapplication;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyReceiverOne extends BroadcastReceiver {    public MyReceiverOne() {    }    @Override    public void onReceive(Context context, Intent intent) {        Log.i("MyReceiverOne", "我是广播接受者MyReceiverOne,接收到了求救广播事件 ");    }}

MyReceiverTwo:

package com.example.thinkpad.myapplication;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyReceiverTwo extends BroadcastReceiver {    public MyReceiverTwo() {    }    @Override    public void onReceive(Context context, Intent intent) {        Log.i("MyReceiverTwo", "我是广播接受者MyReceiverTwo,接收到了求救广播事件 ");    }}

MyReceiverThree:

package com.example.thinkpad.myapplication;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyReceiverThree extends BroadcastReceiver {    public MyReceiverThree() {    }    @Override    public void onReceive(Context context, Intent intent) {        Log.i("MyReceiverThree", "我是广播接受者MyReceiverThree,接收到了求救广播事件 ");    }}

在清单文件中配置:
AndroidMainfest:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.thinkpad.myapplication">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <receiver            android:name=".MyReceiverOne"            android:enabled="true"            android:exported="true">            <intent-filter android:priority="1000">                <action android:name="Intercept_Stitch" />            </intent-filter>        </receiver>        <receiver            android:name=".MyReceiverThree"            android:enabled="true"            android:exported="true">            <intent-filter android:priority="800">                <action android:name="Intercept_Stitch" />            </intent-filter>        </receiver>        <receiver            android:name=".MyReceiverTwo"            android:enabled="true"            android:exported="true">            <intent-filter android:priority="600">                <action android:name="Intercept_Stitch" />            </intent-filter>        </receiver>    </application></manifest>

主要是<intent-filter> 标签里面的内容。

问题1:程序启动后,单击“发送有序广播”按钮,发出一条广播事件,此时观察LogCat窗口下的提示信息,输出什么?为什么?

输出如下:
这里写图片描述
在配置广播接受者时定义的android:priority=“ ”属性,此属性的参数代表广播接受者的优先级,参数大的,优先级就高。
在此案例中,优先级最高的广播MyReceiverOne,最先接收到广播事件,其次是MyReceiverThree,最后是MyReceiverTwo,说明广播接收者的优先级决定了广播接收的先后顺序。

问题2:若将广播接收者MyReceiverTwo优先级同样设置为1000,并将MyReceiverTwo注册在MyReceiverOne前面,再来运行程序,观察结果,你能得出什么结论?

输出如下:
这里写图片描述
从结果可以看出,MyReceiverTwo最先接收到了广播事件,其次是MyReceiverOne,说明当两个广播接受者优先级相同时,先注册的广播接受者会先接收到广播事件。

问题3:修改MyReceiverTwo如下,观察结果,你又可以得出什么结论?

public void onReceive(Context context, Intent intent) {        Log.i("MyReceiverTwo", "我是广播接受者MyReceiverTwo,接收到了求救广播事件 ");        abortBroadcast();        Log.i("MyReceiverTwo", "我是广播接受者MyReceiverTwo,广播被我终结了! ");    }

输出结果如下:
这里写图片描述

从图中可以看出,只有MyReceiverTwo接收到了自定义的广播事件,其他的广播接受者都没有接收到,因此说明广播被MyReceiverTwo终止了。
abortBroadcast()语句用于终止有序广播,当程序执行完这句代码后,广播事件将会被终结,不会向下传递。

原创粉丝点击