上一篇简介了广播的发送,这篇主要介绍下,有序广播的发送。

设置完相关属性的时候,广播就会依照有序的方式进行发送:

发送顺序:

先发送第二条广播;

再发送第一条广播;

最后发送第三条广播。

代码例如以下:

布局文件:

activity_main(一个Button):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="点击发送有序广播" /> </RelativeLayout>

MainActivity:

package com.android_broadcasereceiverdemo2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("myaction");
intent.putExtra("name", "梅西");
sendOrderedBroadcast(intent, null);
}
});
}
}

FirstBroadcast:

package com.android_broadcasereceiverdemo2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; public class FirstBroadcast extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) { String name = intent.getStringExtra("name");
Toast.makeText(context, "第一条广播已发送..."+name, Toast.LENGTH_SHORT).show();
// abortBroadcast();//当加上这条代码的时候,广播发送到此结束,即第三条广播不会再收到。
} }

SecondBroadcast:

package com.android_broadcasereceiverdemo2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; public class SecondBroadcast extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) { String name = intent.getStringExtra("name");
Toast.makeText(context, "第二条广播已发送..."+name, Toast.LENGTH_SHORT).show();
} }

ThirdBroadcast:

package com.android_broadcasereceiverdemo2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; public class ThirdBroadcast extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) { String name = intent.getStringExtra("name");
Toast.makeText(context, "第三条广播已发送..."+name, Toast.LENGTH_SHORT).show();
} }

AndroidManifest.xml(非常关键):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android_broadcasereceiverdemo2"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.android_broadcasereceiverdemo2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <receiver android:name=".FirstBroadcast" >
<!-- 有序广播中,priority数值越大,优先级越大,也就越先发送!! -->
<intent-filter android:priority="99">
<action android:name="myaction" />
</intent-filter>
</receiver> <receiver android:name=".SecondBroadcast" >
<intent-filter android:priority="100">
<action android:name="myaction" />
</intent-filter>
</receiver> <receiver android:name=".ThirdBroadcast" >
<intent-filter android:priority="98">
<action android:name="myaction" />
</intent-filter>
</receiver>
</application> </manifest>

须要源代码的读者能够到我的资源中下载。

Android BroadcastReceiver实例Demo(有序广播的发送)的更多相关文章

  1. Android ListFragment实例Demo(自己定义适配器)

    上一篇文章介绍了ListFragment,当中的ListView并没有自己定义适配器,实际上在实际开发中常会用到自己定义适配器,是实现更复杂的列表数据展示. 所以这篇文章添加了自己定义适配器.来进行L ...

  2. Android ExpandableListView实例Demo

    前几篇文章介绍了Listview.但在实际开发中也常常会用到多层的Listview来展示数据,比方qq中的好友展示,所以这张来了解一下ExpandableListview.基本思想与Listview大 ...

  3. Android广播的发送与接收

    Android广播的发送与接收 效果图 广播发送 广播分为有序广播和无序广播 有序广播与无序广播的区别 无序广播:只要是广播接收者指定了接收的事件类型,就可以接收到发送出来的广播消息.不能修改消息. ...

  4. Android BroadcastReceiver 发送有序广播

    普通广播(Normal Broadcast): 一,优缺点:和有序广播的优缺点相反! 二,发送广播的方法:sendBroadcast() 有序广播(Ordered Broadcast): 一,优缺点 ...

  5. Android学习笔记(十二)BroadcastReceiver的有序广播和优先级

    前两篇博文中简单整理了普通广播,其实还有有序广播,有序广播在开发中也是比不可少的,可以给广播接收者设定优先级来控制接受顺序,并却可以中断广播传递等等. 一.两种Broadcast: · 普通广播(No ...

  6. Android(java)学习笔记179:BroadcastReceiver之 有序广播和无序广播(BroadcastReceiver优先级)

    之前我们在Android(java)学习笔记178中自定义的广播是无序广播,下面我们要了解一下有序广播:   1.   我们首先了解一下有序广播和无序广播区别和联系? (1) 有序广播> 接受者 ...

  7. Android(java)学习笔记122:BroadcastReceiver之 有序广播和无序广播(BroadcastReceiver优先级)

    之前我们在Android(java)学习笔记178中自定义的广播是无序广播,下面我们要了解一下有序广播: 1. 我们首先了解一下有序广播和无序广播区别和联系? (1)有序广播> 接受者有优先级, ...

  8. BroadcastReceiver之有序广播

    有序广播可以按一定的优先级进行传播 首先进行发送广播 public void click(View v){ Intent intent = new Intent(); intent.setAction ...

  9. Android BroadcastReceiver广播接受者

    静态注册 配置清单表注册:只要曾经注册过哪怕关闭也能调用  方式一:sendBroadCastReceive   广播的步骤:       发送  无序广播,普通广播       (1).发送方    ...

随机推荐

  1. Hive学习之四 《Hive分区表场景案例应用案例,企业日志加载》 详解

    文件的加载,只需要三步就够了,废话不多说,来直接的吧. 一.建表 话不多说,直接开始. 建表,对于日志文件来说,最后有分区,在此案例中,对年月日和小时进行了分区. 建表tracktest_log,分隔 ...

  2. C语言内存对齐原理

    一.什么是字节对齐,为什么要对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特定的内存地址访问,这 ...

  3. Python自动化运维之18、Python操作 MySQL、pymysql、SQLAchemy

    一.MySQL 1.概述 什么是数据库 ? 答:数据的仓库,和Excel表中的行和列是差不多的,只是有各种约束和不同数据类型的表格 什么是 MySQL.Oracle.SQLite.Access.MS ...

  4. web ajax跨域问题解决方案

      jsonpHandler({ "code": "aaa", "price": 1780, "tickets": 5 ...

  5. JS键盘的键码(event.keyCode)

    keycode 8 = BackSpace BackSpace keycode 9 = Tab Tab keycode 12 = Clear keycode 13 = Enter keycode 16 ...

  6. PQJ 1686(栈栈栈)

    PQJ  1686(栈栈栈) 用栈解决问题 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  7. jquery-te 轻量级的编辑器

  8. 关于.net的概念

    IIS不仅仅是asp.net还有php等 所以w3wp是基本单元. 然后Managed Code加载 AppDomain, 不管是桌面程序还是asp.net程序 System.Web(HttpRunt ...

  9. Android 首次进入应用时加载引导界面

    功能需求:首次进入应用时加载引导界面 思路: 1.首次进入,怎么判断?查看SharedPreferences中某个字段 2.基本上每个应用都有个进入实际功能是的动画加载页面,我们可以在该Activit ...

  10. Oracle 用户权限管理

    SQL> select * from ROLE_SYS_PRIVS where ROLE='RESOURCE'; ROLE PRIVILEGE ADM --------------------- ...