BroadcastReceiver是Android系统的四大组件之一,BroadcastReceiver是一个全局的系统级监听器,它拥有自己的独立进程。

我们来写一个最简单的广播接收过程

先在manifest中定义一个广播接受者

    <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.broadcasttest.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="com.example.broadcasttest.MyBroadCast">
<intent-filter >
<action android:name="com.meritit.action.MY_BROADCAST"/>
</intent-filter>
</receiver>
</application>

广播接收者

public class MyBroadCast extends BroadcastReceiver{

	@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "接收到的值为:" + intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show();
} }

向广播接收者发送广播

package com.example.broadcasttest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setAction("com.meritit.action.MY_BROADCAST");
intent.putExtra("msg", "阳光小强");
sendBroadcast(intent);
}
});
}
}

运行结果:

上面的例子是一个普通的广播接受者,下面我们来修改一下代码看看有序的广播接受者

        <receiver
android:name="com.example.broadcasttest.MyBroadCast">
<intent-filter android:priority="20">
<action android:name="com.meritit.action.MY_BROADCAST"/>
</intent-filter>
</receiver>
<receiver
android:name="com.example.broadcasttest.MyBroadCast2">
<intent-filter android:priority="0" >
<action android:name="com.meritit.action.MY_BROADCAST"/>
</intent-filter>
</receiver>

两个广播接收者设置了优先级,上面的优先级比下面的高

Intent intent = new Intent();
intent.setAction("com.meritit.action.MY_BROADCAST");
intent.putExtra("msg", "阳光小强");
sendOrderedBroadcast(intent, null);

发送有序广播,注意是sendOrderedBroadcast

public class MyBroadCast extends BroadcastReceiver{

	@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "接收到的值为:" + intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("msg2", "有序的广播");
setResultExtras(bundle); //如果不想继续传播
//abortBroadcast();
} }

优先级高的MyBroadCast先接收到,有序广播接收者可以添加新数据给下个等级的接受者。这种形式就有点像拦截器。

public class MyBroadCast2 extends BroadcastReceiver{

	private static final String TAG = "broadcast";

	@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = getResultExtras(true);
String msg = intent.getStringExtra("msg");
String msg2 = bundle.getString("msg2");
Log.i(TAG, msg);
Log.i(TAG, msg2);
} }

最后输出结果:


除了接收用户发送的广播之外,BroadcastReceiver还有一个重要作用,就是接收系统广播。

详细请看:http://developer.android.com/reference/android/content/Intent.html

下面列出Android常见的广播。

例如检测电池电量过低

        <receiver
android:name="com.example.broadcasttest.MyBroadCast3">
<intent-filter >
<action android:name="android.intent.action.ACTION_BATTERY_LOW"/>
</intent-filter>
</receiver>

public class MyBroadCast3 extends BroadcastReceiver{

	@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
int current = bundle.getInt("level");
int total = bundle.getInt("scale");
if(current * 1.0 / total < 0.15){
Toast.makeText(context, "电池电量过低,请尽快充电", Toast.LENGTH_LONG).show();
}
} }

Android菜鸟的成长笔记(26)——普通广播与有序广播的更多相关文章

  1. Android菜鸟的成长笔记(3)——给QQ登录界面说So Easy

    原文:Android菜鸟的成长笔记(3)--给QQ登录界面说So Easy 上一篇:Android菜鸟的成长笔记(2)--第一个Android应用 我们前面已经做了第一个Android应用程序,虽然有 ...

  2. Android菜鸟的成长笔记(2)——第一个Android应用

    原文:Android菜鸟的成长笔记(2)--第一个Android应用 上一篇:Android菜鸟的成长笔记(1)--Anddroid环境搭建从入门到精通 在上一篇Android菜鸟的成长笔记(1)中我 ...

  3. Android菜鸟的成长笔记(1)——Android开发环境搭建从入门到精通

    原文:Android菜鸟的成长笔记(1)--Android开发环境搭建从入门到精通 今天在博客中看到好多Android的初学者对Android的开发环境的搭建不熟悉而导致不能进行学习,所以我决定自己写 ...

  4. Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上)

    原文:[置顶] Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上) 我们在用手机的时候可能会发现,即使应用被放到后台再返回到前台数据依然保留(比如说我们正在玩游戏,突然电话 ...

  5. Android菜鸟的成长笔记(13)——异步任务(Async Task)

    原文:[置顶] Android菜鸟的成长笔记(13)——异步任务(Async Task) Android的UI线程主要负责处理用户的事件及图形显示,因此主线程UI不能阻塞,否则会弹出一个ANR(App ...

  6. Android菜鸟的成长笔记(12)——Handler、Loop、MessageQueue

    原文:[置顶] Android菜鸟的成长笔记(12)——Handler.Loop.MessageQueue 当一个程序第一次启动时,Android会启动一条主线程(Main Thread),主线程主要 ...

  7. Android菜鸟的成长笔记(11)——Android中的事件处理

    原文:[置顶] Android菜鸟的成长笔记(11)——Android中的事件处理 Android提供了两种方式来处理事件,一个是基于回调的事件处理,另一个是基于监听的事件处理,举个例子: 基于回调的 ...

  8. Android菜鸟的成长笔记(10)——使用Bundle在Activity之间传值

    原文:[置顶] Android菜鸟的成长笔记(10)——使用Bundle在Activity之间传值 前面我们了解了如何启动一个Activity,一个Activity在启动另外一个Activity的时候 ...

  9. Android菜鸟的成长笔记(9)——Intent与Intent Filter(下)

    原文:[置顶] Android菜鸟的成长笔记(9)——Intent与Intent Filter(下) 接着上一篇的内容,下面我们再来看看Intent的Data与Type属性. 一.Data属性与Typ ...

  10. Android菜鸟的成长笔记(8)——Intent与Intent Filter(上)

    原文:[置顶] Android菜鸟的成长笔记(8)——Intent与Intent Filter(上) Intent代表了Android应用的启动“意图”,Android应用将会根据Intent来启动指 ...

随机推荐

  1. Bag of Features (BOF)图像检索算法

    1.首先.我们用surf算法生成图像库中每幅图的特征点及描写叙述符. 2.再用k-means算法对图像库中的特征点进行训练,生成类心. 3.生成每幅图像的BOF.详细方法为:推断图像的每一个特征点与哪 ...

  2. D3.js中对array的使用

    由于D3类库和array密切相关,我们有必要讨论一下D3中的数据绑定以及在数组内部运算的方法. 1.D3中的数组 和其他编程语言一样,D3的数组元素可以是数字或者字符等类型,例如: someData= ...

  3. AVCaptureSession音频视频采集

    // // AudioVideoCaptureViewController.m // live // // Created by lujunjie on 2016/10/31. // Copyrigh ...

  4. [Mobx] Use MobX actions to change and guard state

    This lesson explains how actions can be used to control and modify the state of your application. Th ...

  5. Java设计模式——代理模式实现及原理

    简介 Java编程的目标是实现现实不能完成的,优化现实能够完成的,是一种虚拟技术.生活中的方方面面都可以虚拟到代码中.代理模式所讲的就是现实生活中的这么一个概念:中介. 代理模式的定义:给某一个对象提 ...

  6. (转)windows 下 Java 及 Python 环境变量设置

    转自:http://www.cnblogs.com/zhj5chengfeng/archive/2013/01/01/2841253.html http://www.cnblogs.com/qiyes ...

  7. Day2:字典

    一.定义 字典是一种“key-value”成对出现的数据类型,中间用冒号把key与value隔,不同的数据用逗号隔开,全部数据用大括号括起来 info = { 'stu1101': "Ten ...

  8. 怎样用Adobe Acrobat 7 Pro把PDF文档拆分成多个啊?

    这个pdf文档里有多篇文章,我想把他们分开并分别保存在独立的pdf文档.怎么操作?我的电脑基础不太好,麻烦说得详细一些. Adobe Acrobat 7 Pro拆分PDF文档的方法: 1.点左边的“书 ...

  9. html练习(3)

    1.这个小练习用到了css的四种选择器id选择器,类选择器,html选择器,通配符选择器. (1)假设一个元素中用到了各种选择器,而且选择器中的属性发生了冲突,则 优先级为id选择器>类选择器& ...

  10. WPF学习笔记——概述

    如果你选择WPF,多半原因是因为折服于它那震撼性的用户体验.纵观WPF整个知识体系,其内容并不复杂,但却比较细碎,不易理清.以下内容是对WPF部分内容的简单概括,希望读者能够对WPF框架有个大体认识. ...