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

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

发送顺序:

先发送第二条广播;

再发送第一条广播;

最后发送第三条广播。

代码例如以下:

布局文件:

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. Oracle 创建分页存储过程(转帖)

    原贴地址:http://19880614.blog.51cto.com/4202939/1316560 ps:源代码还有很多错误,我修改了 ------------------------------ ...

  2. [转] 与调试器共舞 - LLDB 的华尔兹

    你是否曾经苦恼于理解你的代码,而去尝试打印一个变量的值? NSLog(@"%@", whatIsInsideThisThing); 或者跳过一个函数调用来简化程序的行为? NSNu ...

  3. Objective-C学习篇05—Foundation框架简介

    iOS中所谓的框架,说到底就是一个目录,iOS提供了很多我们可以在应用程序中调用的框架.许多应用程序都使用了如Foundation.UIKit和Core Graphics这些框架.根据你为应用程序选择 ...

  4. Hibernate 环境搭建

    Hibernate 工作流程 1.创建工程并导包 2.在src根目录下创建配置文件:hibernate.cfg.xml(也可以创建在src其他文件夹下,但是在后面的配置中,需要指明路径) <?x ...

  5. Linux文件编程实例

    //捕获fopen调用中的错误 #include <stdio.h> #include <errno.h> #include <string.h> #define  ...

  6. 03_RHEL7.1去掉注册提示

    # rpm –qa|grep subscription-manager 出现类似下面的代码: subscription-manager-firstboot-1.13.22-1.el7.x86_64 s ...

  7. 修改本地数据库root权限密码

    方法1: 用SET PASSWORD命令 测试成功 首先登录MySQL @1——mysql DOS 窗口中. 格式:mysql> set password for 用户名@localhost = ...

  8. Python里的拷贝=====》很容易错误的

    不能直接用 = 复制: import copy a = [1, 2, 3, 4, ['a', 'b']] #原始对象 b = a #赋值,传对象的引用 c = copy.copy(a) #对象拷贝,浅 ...

  9. UIImageView设置为圆形

       CGFloat headimageX = self.view.frame.size.width * 0.2; CGFloat headimageY = self.view.frame.size. ...

  10. 「30天自制操作系统」 Stop & 「OS67 」 Start

    废话 整个十月都没有再写一点什么, 其实没什么好写的, 把书里的东西码出来贴在博客里实在没什么意思, 况且书里已经写得够详细了. 这本书给我最深刻的感觉是, 作者通过简化一些细节, 一步一步地模拟整个 ...