发送有序广播

1.运行效果图

发送有序广播_第1张图片

2.训练目标

掌握有序广播的使用

3. 运行程序,回答问题

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

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

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

实现该案例的具体步骤如下:

1.创建程序
创建一个名为”OrderedBroadcast”的应用程序,包名为”cn.edu.bzu.orderedbroadcast”。设计用户交互界面,如图:
发送有序广播_第2张图片

对应布局文件(activity_main.xml)如下:


<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"
   android:background="@drawable/stitch_one"
    tools:context="cn.edu.bzu.orderedbroadcast.MainActivity">


    <Button
        android:text="发送有序广播"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:onClick="send"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:background="#FBFBFF"
        android:textSize="20sp"
        android:id="@+id/button" />
RelativeLayout>

2.编写界面交互代码(MainActivity)

package cn.edu.bzu.orderedbroadcast;

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);
    }

}

3.添加广播接收者
添加三个广播接收者MyBroadcastReceiverOne,MyBroadcastReceiverTwo,MyBroadcastReceiverThree,不同的广播接收者打印不同的信息。

MyBroadcastReceiverOne的代码如下:

package cn.edu.bzu.orderedbroadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by Administrator on 2017/6/2.
 */

public class MyBroadcastReceiverOne extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcastReceiverOne","自定义的广播接收者One,接受到了广播事件");
    }
}

MyBroadcastReceiverTwo的代码如下:

package cn.edu.bzu.orderedbroadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by Administrator on 2017/6/2.
 */

public class MyBroadcastReceiverTwo extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接受到了广播事件");
    }
}

MyBroadcastReceiverThree的代码如下:

package cn.edu.bzu.orderedbroadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by Administrator on 2017/6/2.
 */

public class MyBroadcastReceiverThree extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcastReceiverThre","自定义的广播接收者Three,接受到了广播事件");
    }
}

3.配置清单文件

<receiver android:name=".MyBroadcastReceiverOne">
            <intent-filter android:priority="1000">
                <action android:name="Intercept_Stitch"/>
            intent-filter>
        receiver>
        <receiver android:name=".MyBroadcastReceiverTwo">
            <intent-filter android:priority="200">
                <action android:name="Intercept_Stitch"/>
            intent-filter>
        receiver>
        <receiver android:name=".MyBroadcastReceiverThree">
            <intent-filter android:priority="600">
                <action android:name="Intercept_Stitch"/>
            intent-filter>
        receiver>
    application>

4.运行程序拦截有序广播
程序启动后,单击“发送有序广播”按钮,控制台输出如图所示信息:
发送有序广播_第3张图片
发送有序广播_第4张图片

5.修改清单文件
(1)将广播接收者MyBroadcastReceiverTwo优先级同样设置为1000,控制台输出如图所示信息:
发送有序广播_第5张图片

(2)修改MyBroadcastReceiverTwo,代码如下:

 public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接受到了广播事件");
        abortBroadcast();
        Log.i("MyBroadcastReceiverTwo","我是广播接收者Two,广播被我终结了");
    }

运行程序,控制台输出如下:
发送有序广播_第6张图片

你可能感兴趣的