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. ViewPager+Fragmrnt最简单结合方法

    Fragment和ViewPager 本博文系本菜鸟第一次博文展示,有错误之处请虽然提出 FragmentPagerAdapter 谷歌官方提供了这么一个adapter(FragmentPagerAd ...

  2. 1.1 Introduction中 Producers官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Producers 生产者(Producers) Producers publish ...

  3. hibernate 注解配置<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/X

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. 【河南省多校脸萌第六场 A】巴什博弈?

    [链接]http://acm.nyist.me/JudgeOnline/problem.php?cid=1013&pid=5 [题意] 在这里写题意 [题解] 0..a-1 YES a..a+ ...

  5. js进阶 13 jquery动画函数有哪些

    js进阶 13 jquery动画函数有哪些 一.总结 一句话总结: 二.jquery动画函数有哪些 原生JavaScript编写动画效果代码比较复杂,而且还需要考虑兼容性.通过jQuery,我们使用简 ...

  6. UVA 11800 - Determine the Shape 几何

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  7. 第一个Python程序(全面)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一.Windows系统 1.编写Python程序方式之Sublime文本编辑器: 1>打开sublime,创建hello.p ...

  8. php课程 8-30 实现验证码验证的难点是什么

    php课程 8-30 实现验证码验证的难点是什么 一.总结 一句话总结:session技术实现验证码传递. 1.生成验证码的那个网页(php文件)中的验证码怎么搁到别的网页中去? 直接在img的src ...

  9. orabbix 报错No suitable driver found for

     orabbix报错如下:   2018-07-11 14:35:20,119 [main] ERROR Orabbix - Error on Configurator for database qa ...

  10. mootools常用特性和示例(基础篇2)

    接着上一篇:mootools常用特性和示例(基础篇1) 1.表单操作 html: <form id="myForm" action="submit.php" ...