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).发送方 ...
随机推荐
- Swift - 05 - 数值型字面量
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- C#串口控制
串行口是计算机的标准接口,现在的PC机(个人电脑)一般至少有两个串行口COM1和COM2.串行口应用广泛,在数据通信.计算机网络以及分布式工业控制系统中,经常采用串行通信来交换数据和信息.本节通过几个 ...
- cas sso入门(转)
转:http://blog.csdn.net/frinder/article/details/7969925 一.教程说明 前言 教程目的:从头到尾细细道来单点登录服务器及客户端应用的每个步骤 单点登 ...
- [转]Java中byte与16进制字符串的互相转换
Java中byte用二进制表示占用8位,而我们知道16进制的每个字符需要用4位二进制位来表示(23 + 22 + 21 + 20 = 15),所以我们就可以把每个byte转换成两个相应的16进制字符, ...
- inputtype
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content&q ...
- 【USACO 3.1.3】丑数
[描述] 对于一给定的素数集合 S = {p1, p2, ..., pK}, 来考虑那些质因数全部属于S 的数的集合.这个集合包括,p1, p1p2, p1p1, 和 p1p2p3 (还有其它).这是 ...
- 『重构--改善既有代码的设计』读书笔记----Introduce Explaning Variable
有时候你会遇到一系列复杂的表达式连续运算的时候,这个时候你可能根本招架不住如此长或者是如此复杂的长函数.这个时候你可以通过引用临时变量来储存他们的结果,将这些长函数的结果分成一个个临时变量来让函数清晰 ...
- C#的SerialPort串口程序设计总结
简介:微软的VS提供了SerialPort控件,也就是串行端口资源. 当然也可以添加引用 using System.IO.Ports; 通过实例化SerialPort对象就可以使用其属性和方法了. S ...
- IOS快速开发之常量定义
---恢复内容开始--- 在IOS开发中,有一些方法常常需要用的,但是有很长的方法名,这造成了代码长,写起来累,我们可以通过宏定义了解决这些问题 比如说在代码布局的时候会遇上这样的问题,我们要获取上面 ...
- 黑马程序员—C语言的函数、数组、字符串
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 一.函数 定义:函数是代码复用的一种机制. 函数的基本语法: 返回类型 函数名 ( 参数类型 参 ...