Android BroadcastReceiver实例Demo(有序广播的发送)
上一篇简介了广播的发送,这篇主要介绍下,有序广播的发送。
设置完相关属性的时候,广播就会依照有序的方式进行发送:
发送顺序:
先发送第二条广播;
再发送第一条广播;
最后发送第三条广播。
代码例如以下:
布局文件:
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(有序广播的发送)的更多相关文章
- Android ListFragment实例Demo(自己定义适配器)
上一篇文章介绍了ListFragment,当中的ListView并没有自己定义适配器,实际上在实际开发中常会用到自己定义适配器,是实现更复杂的列表数据展示. 所以这篇文章添加了自己定义适配器.来进行L ...
- Android ExpandableListView实例Demo
前几篇文章介绍了Listview.但在实际开发中也常常会用到多层的Listview来展示数据,比方qq中的好友展示,所以这张来了解一下ExpandableListview.基本思想与Listview大 ...
- Android广播的发送与接收
Android广播的发送与接收 效果图 广播发送 广播分为有序广播和无序广播 有序广播与无序广播的区别 无序广播:只要是广播接收者指定了接收的事件类型,就可以接收到发送出来的广播消息.不能修改消息. ...
- Android BroadcastReceiver 发送有序广播
普通广播(Normal Broadcast): 一,优缺点:和有序广播的优缺点相反! 二,发送广播的方法:sendBroadcast() 有序广播(Ordered Broadcast): 一,优缺点 ...
- Android学习笔记(十二)BroadcastReceiver的有序广播和优先级
前两篇博文中简单整理了普通广播,其实还有有序广播,有序广播在开发中也是比不可少的,可以给广播接收者设定优先级来控制接受顺序,并却可以中断广播传递等等. 一.两种Broadcast: · 普通广播(No ...
- Android(java)学习笔记179:BroadcastReceiver之 有序广播和无序广播(BroadcastReceiver优先级)
之前我们在Android(java)学习笔记178中自定义的广播是无序广播,下面我们要了解一下有序广播: 1. 我们首先了解一下有序广播和无序广播区别和联系? (1) 有序广播> 接受者 ...
- Android(java)学习笔记122:BroadcastReceiver之 有序广播和无序广播(BroadcastReceiver优先级)
之前我们在Android(java)学习笔记178中自定义的广播是无序广播,下面我们要了解一下有序广播: 1. 我们首先了解一下有序广播和无序广播区别和联系? (1)有序广播> 接受者有优先级, ...
- BroadcastReceiver之有序广播
有序广播可以按一定的优先级进行传播 首先进行发送广播 public void click(View v){ Intent intent = new Intent(); intent.setAction ...
- Android BroadcastReceiver广播接受者
静态注册 配置清单表注册:只要曾经注册过哪怕关闭也能调用 方式一:sendBroadCastReceive 广播的步骤: 发送 无序广播,普通广播 (1).发送方 ...
随机推荐
- MySQL 创建数据表
MySQL 创建数据表 创建MySQL数据表需要以下信息: 表名 表字段名 定义每个表字段 语法 以下为创建MySQL数据表的SQL通用语法: CREATE TABLE table_name (col ...
- C++ Primer 5th 第11章 关联容器
练习11.1:描述map 和 vector 的不同. map是关联容器,vector是顺序容器,关联容器与值无关,vector则与值密切相关 练习11.2:分别给出最适合使用 list.vector. ...
- jQuery 选择器和JavaScript 选择器的技巧与异常原因
jquery的选择器借鉴了css选择器,核心依然依靠JavaScript的getElementById()和getElementsByTagName()方法,但是他封装了2个方法,让jquery选择器 ...
- javascript读取xml的方法【转载】
jquery读取xml文件 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...
- javascript权威指南学习笔记2
Javascript语言核心(2~12章) 第三章:类型.值.变量 1.数字: overflow(Infinity, -Infinity).underflow(+0,-0) 非数字值:它和任何值都不相 ...
- jQuery自带的一些常用方法总结
jQuery自带的一些常用方法总结 这篇文章主要介绍了jQuery自带的一些常用方法总结,包括$.trim .$.contains.$.each.$.map.$.inArray.$.extend等,需 ...
- ucenter 通信原理
1.用户登录discuz,通过logging.php文件中的函数uc_user_login对post过来的数据进行验证,也就是对username和password进行验证. 2.如果验证成功,将调用位 ...
- CodeForces 569A 第六周比赛C踢
C - C Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- Oracle的Net Configuration Assistant 配置
在进行团队开发的时候,一般团队的每一个人只需要安装一个客户端即可,没有必要安装一个Oracle 数据库服务器,而数据库服务器是属于共享的,此时,我们就需要配置客户端.客户端的配置可以有以下两种方式:第 ...
- select查询的性能
为什么忘记commit也会造成select查询的性能问题 今天遇到一个很有意思的问题,一个开发人员反馈在测试服务器ORACLE数据库执行的一条简单SQL语句非常缓慢,他写的一个SQL没有返回任何数据, ...