Android中发送有序广播案例代码

所属分类: 软件编程 / Android 阅读数: 50
收藏 0 赞 0 分享

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没有接收到广播事件,也就没有打印关于它们的信息。

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

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多