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

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

发送顺序:

先发送第二条广播;

再发送第一条广播;

最后发送第三条广播。

代码例如以下:

布局文件:

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. CoreLocation+MapKit系统定位(含坐标以及详细地址)

    iOS8 之后出现一些新的配置 [self.manager requestWhenInUseAuthorization]; 并且在info.plist文件中增加 NSLocationWhenInUse ...

  2. 如何修改UITableView每个cell的分隔线和左边的距离?

    在ios7中,UITableViewCell左侧会有默认15像素的空白.这时候,设置setSeparatorInset:UIEdgeInsetsZero 能将空白去掉.但是在ios8中,设置setSe ...

  3. 更改tabBarItem图片的问题

    代码: UIImage *normal = [[UIImage imageNamed:@"tabbar_home_default"] imageWithRenderingMode: ...

  4. angular自定义指令详解

    指令(directive)是angular里面最核心也是最难懂的东西,在慕课网看了下大漠穷秋老湿的视频,自己百度半天做了一些小test,总算把一切都搞明白了. 先列出学习来源: 指令中controll ...

  5. poj3278 BFS入门

    M - 搜索 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536KB     64bit I ...

  6. Yii2的相关学习记录,初始化Yii2(二)

    前面已经将Yii2下载下来了,那我们就需要能实际的使用. 一.初始化,因为我都是在windows系统下,所以用cmd命令打开下载下来的Yii2的根目录.然后运行下面命令: init 会提示选择0为开发 ...

  7. jquery学习(2)toggle

    $(function(){ $("#panel h5.head").hover(function(){ //交替执行该函数 $(this).next().show(); },fun ...

  8. java常用用代码

    /** *Java获取IP代码 */ import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.ev ...

  9. apache静态文件配置

    开发环境配置 需要下面几个步骤 1. 在app目录下创建static目录,将静态文件和相关文件夹放到此目录下,如your_app/static/img等 2. 确保settings.py中的INSTA ...

  10. HDU 1005(周期问题)

    HDU 1005 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descript ...