这里有新鲜出炉的精品教程,程序狗速度看过来!
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
本篇文章主要介绍了 Android 中发送有序广播案例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
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 没有接收到广播事件,也就没有打印关于它们的信息。
来源: http://www.phperz.com/article/17/0704/336124.html