Android系统的4个组件最终还剩一种组件了BroadcastReceiver,这个组件是全局监听器,能够监听系统全局的广播消息,能够方便的实现系统中不同组件之间的通信

BroadcastReceiver有自己的进程,系统级监听器,仅仅要存在与之匹配的Intent被广播出来,BroadcastReceiver就会被激发

要创建自己的BroadcastReceiver对象,我们须要继承android.content.BroadcastReceiver,并实现其onReceive方法

MyReceiver.java

public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context,
"接收到的Intent的Action为:" + intent.getAction()
+ "\n消息内容是:" + intent.getStringExtra("msg")
, Toast.LENGTH_LONG).show();
}
}

Manifest.xml清单文件配置的receiver

<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="org.crazyit.action.CRAZY_BROADCAST" />
</intent-filter>
</receiver>

就是说不管哪个组件中,intent的Action是"org.crazyit.action.CRAZY_BROADCAST" 并使用使用sendBroadcast(intent)发出广播,那么MyReceiver就会被启动

public class BroadcastMain extends Activity
{
Button send; @Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取程序界面中的button
send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// 创建Intent对象
Intent intent = new Intent();
// 设置Intent的Action属性
intent.setAction("org.crazyit.action.CRAZY_BROADCAST");
intent.putExtra("msg", "简单的消息");
// 发送广播
sendBroadcast(intent);
}
});
}
}

注冊Receiver有两种方法:

静态注冊

静态注冊是在AndroidManifest.xml文件里配置的。我们就来为MyReceiver注冊一个广播地址:

<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.MY_BROADCAST"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

配置了以上信息之后。仅仅要是android.intent.action.MY_BROADCAST这个地址的广播,MyReceiver都可以接收的到。注意,这样的方式的注冊是常驻型的。也就是说当应用关闭后,假设有广播信息传来,MyReceiver也会被系统调用而自己主动执行。

动态注冊

动态注冊须要在代码中动态的指定广播地址并注冊,通常我们是在Activity或Service注冊一个广播,以下我们就来看一下注冊的代码:

MyReceiver receiver = new MyReceiver();  

IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.MY_BROADCAST"); registerReceiver(receiver, filter); //注冊<pre name="code" class="java">receiver 和filter

普通广播

普通广播对于多个接收者来说是全然异步的。通常每一个接收者都无需等待即能够接收到广播,接收者相互之间不会有影响。

对于这样的广播。接收者无法终止广播。即无法阻止其它接收者的接收动作。

上面的样例就是发送的普通广播

有序广播

有序广播比較特殊,它每次仅仅发送到优先级较高的接收者那里,然后由优先级高的接受者再传播到优先级低的接收者那里。优先级高的接收者有能力终止这个广播。

比如:优先级A>B>C,Broadcast先传给A。再传给B,在传给C。优先级别声明<Intent-filter>元素的android:priority中。数越大级别越高,取值范围在-1000~1000

优先收到Broadcast的接受者能够通过setResultExtras(Bundle)方法将处理结果存入Broadcast中,然后传给下一个接受者。通过Bundle bunde=getResultExtras(true)柯获得上一个接受者存入的数据

public class SortedBroadcast extends Activity
{
Button send; @Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取程序中的sendbutton
send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// 创建Intent对象
Intent intent = new Intent();
intent.setAction("org.crazyit.action.CRAZY_BROADCAST");
intent.putExtra("msg", "简单的消息");
// 发送有序广播
sendOrderedBroadcast(intent, null);
}
});
}
}

MyReceiver.java

public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "接收到的Intent的Action为:"
+ intent.getAction() + "\n消息内容是:"
+ intent.getStringExtra("msg")
, Toast.LENGTH_LONG).show();
// 创建一个Bundle对象,并存入数据
Bundle bundle = new Bundle();
bundle.putString("first", "第一个BroadcastReceiver存入的消息");
// 将bundle放入结果中
setResultExtras(bundle);
// 取消Broadcast的继续传播
// abortBroadcast(); //①
}
}

MyReceiver2.java

public class MyReceiver2 extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = getResultExtras(true);
// 解析前一个BroadcastReceiver所存入的key为first的消息
String first = bundle.getString("first");
Toast.makeText(context, "第一个Broadcast存入的消息为:"
+ first, Toast.LENGTH_LONG).show();
}
}

清单文件

<receiver android:name=".MyReceiver">
<intent-filter android:priority="20">
<action android:name="org.crazyit.action.CRAZY_BROADCAST" />
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver2">
<intent-filter android:priority="0">
<action android:name="org.crazyit.action.CRAZY_BROADCAST" />
</intent-filter>
</receiver>

Android中BroadcastReceiver组件具体解释的更多相关文章

  1. Android中BroadcastReceiver的两种注册方式(静态和动态)详解

    今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...

  2. android中四大组件之间相互通信

    好久没有写有关android有关的博客了,今天主要来谈一谈android中四大组件.首先,接触android的人,都应该知道android中有四大组件,activity,service,broadca ...

  3. Android中Intent组件详解

    Intent是不同组件之间相互通讯的纽带,封装了不同组件之间通讯的条件.Intent本身是定义为一个类别(Class),一个Intent对象表达一个目的(Goal)或期望(Expectation),叙 ...

  4. Android中的动画具体解释系列【4】——Activity之间切换动画

    前面介绍了Android中的逐帧动画和补间动画,并实现了简单的自己定义动画.这一篇我们来看看怎样将Android中的动画运用到实际开发中的一个场景--Activity之间跳转动画. 一.定义动画资源 ...

  5. Android中时间戳的详细解释

    Android中时间戳的详细解释: (1).定义: 时间戳就是根据当前系统时间生成的一组随机数字. (2).作用: 作为对数据唯一性的一种判断依据.避免了重复修改数据所带来的错误! (3).应用: ( ...

  6. Android中BroadcastReceiver广播

    BroadCastReceiver 简介 广播接收者( BroadcastReceiver )用于接收广播 Intent ,广播 Intent 的发送是通过调用 Context.sendBroadca ...

  7. Android的BroadcastReceiver组件

    BroadcastReceiver的作用: BroadcastReceiver,是和Intent有很大关系的Android组件. Android中的 Intent 可以用来: 1. 在应用程序内部和应 ...

  8. Android中BroadcastReceiver的使用

    1.Android中广播分为静态注册和动态注册 2.下面是一个简单静态注册的例子 创建一个继承BroadcastReceiver的子类 public class DeviceBootReceiver ...

  9. Android中ViewStub组件使用

    1. 概述: ViewStub组件和<include>标签的作用类似,主要是为了提高布局的重用性,及布局的模块化.它们之间最大的差别是,ViewStub中的布局不会随着它所在布局的渲染而渲 ...

随机推荐

  1. bootstrap model弹出框的使用

    之前,我们讲解了bootstrap tab的使用,今天我们来了解下bootstrap 中model弹出窗的使用. 效果: 代码:<input id="btntext" typ ...

  2. ffmpeg Win8移植记(一)

    最近和同事一起合作,移植ffmepg到Win8平台上. Windows Store 要求3个架构X86 X64 ARM, 我们主要做的就是X86和ARM的平台, X86的平台移植的文章已经很多了.我推 ...

  3. Spring 泛型依赖注入

    BaseService<T>:有RoleService和UserService两的子类 BaseRepepositry<T>:有UserRepository和RoleRepos ...

  4. Linux内核开发之异步通知与异步I/O(一)

    “小王,听说过锦上添花吧..”我拍拍下王的头说. “还锦上添花你,为你上次提的几个东东,我是头上长包..”小王气愤地瞪着我. “啊,为啥这样呢,本来还特意拒绝了MM的月份,抽出时间打算给你说点高级的东 ...

  5. springmvc sitemesh json问题

    参考: 解决方法: <sitemesh> <mapping path="/*" decorator="/WEB-INF/views/template/t ...

  6. 主从复制时报:ERROR 1794 (HY000): Slave is not configured or failed to initialize properly. You must at least set --server-id to enable either a master or a slave. Additional error messages can be found in t

    centos 6.5 mysql5.7 在从库作stop slave时报: error:ERROR 1794 (HY000): Slave is not configured or failed to ...

  7. MySQL-锁研究

    隔离级别研究: http://www.cnblogs.com/JohnABC/p/3521061.html 表级:引擎 MyISAM, 理解为锁住整个表, 锁定期间, 其它进程无法对该表进行写操作, ...

  8. WCF学习之三, 寄宿方式 代码,配置文件

    可以通过代码或者配置文件寄宿WCF服务,在使用过程中的一些心得,记录一下,方便后续查阅. 预备知识,几个地址的作用 1. behavior.HttpGetUrl  定义元数据的地址,如果不定义基地址, ...

  9. C#:Ini文件操作(待补充)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...

  10. Python 爬虫实例(4)—— 爬取网易新闻

    自己闲来无聊,就爬取了网易信息,重点是分析网页,使用抓包工具详细的分析网页的每个链接,数据存储在sqllite中,这里只是简单的解析了新闻页面的文字信息,并未对图片信息进行解析 仅供参考,不足之处请指 ...