Android系统提供了两种广播类型,一种是有序广播,一种是有序广播。

(1)无序广播是完全异步执行,发送广播时所有监听这个广播的广播接收者都会收到此消息,但接收的顺序不确定。

(2)有序广播是按照接收者的优先级接收,只有一个广播接收者能接收信息,在此广播接收者中逻辑执行完毕后,才会继续传递。

实验要求:通过sendOrderedBroadeast()发送一条有序广播

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

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:onClick="send"
    android:layout_marginTop="50dp"
    android:text="发送有序广播"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:background="#FFD202"
    android:textSize="20sp"
    />
</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.创建三个广播接收者

(1)MyBroadcastReceiverOne

(2)MyBroadcastReceiverTwo

(3)MyBroadcastReceiverThree

(1)在MyBroadcastReceiverOne中代码如下:

package cn.edu.bzu.orderedbroadcast;

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

public class MyBroadcastReceiverOne extends BroadcastReceiver {
  public MyBroadcastReceiverOne() {
  }

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

(2)在MyBroadcastReceiverTwo中代码如下:

package cn.edu.bzu.orderedbroadcast;

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

public class MyBroadcastReceiverTwo extends BroadcastReceiver {
  public MyBroadcastReceiverTwo() {
  }

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

(3)在MyBroadcastReceiverThree中代码如下:

package cn.edu.bzu.orderedbroadcast;

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

public class MyBroadcastReceiverThree extends BroadcastReceiver {
  public MyBroadcastReceiverThree() {
  }

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

4.在配置文件AndroidManifest中代码如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="cn.edu.bzu.orderedbroadcast">

  <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=".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>

</manifest>

5.实验效果图:

运行程序后解决如下问题:

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

点击按钮后出现如下图所示提示信息,原因是有序广播根据优先级接收广播信息的,在配置文件中广播接收者One的priority值最大,所以它先接收到广播,其次是Three,最后是Two。其中android:priority="值"是用来设置广播接收者的优先级的,值越大,优先级别越高。

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

修改配置文件AndroidManifest中代码如下:

<receiver
  android:name=".MyBroadcastReceiverTwo">
  <intent-filter android:priority="1000">
    <action android:name="Intercept_Stitch"/>
  </intent-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiverOne">
  <intent-filter android:priority="1000">
    <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> 

运行程序出现如下图所示提示信息,原因是当广播接收者的优先级别相同时,先注册的广播接收者先接收到广播。

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

public class MyBroadcastReceiverTwo extends BroadcastReceiver {
  public MyBroadcastReceiverTwo() {
  }

  @Override
   public void onReceive(Context context, Intent intent) {
    Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件");
    abortBroadcast();//有序广播拦截器
    Log.i("MyBroadcastReceiverTwo","我是广播接收者Two,广播被我终止了");
  }

} 

运行程序出现如下图所示提示信息,原因是广播接收者Two的优先级最高,所以在Two中写一个拦截器,优先级低的广播接收者将接收不到广播信息,所以One和Three没有接收到广播事件,也就没有打印关于它们的信息。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

02-08 21:59