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. POJ——T 1006 Biorhythms

    http://poj.org/problem?id=1006 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 138219   ...

  2. [D3JS] Add more map layer and color

    import React, {Component} from 'react'; import * as d3 from 'd3'; import 'd3-geo'; import * as topoj ...

  3. 【Codeforces Round #450 (Div. 2) A】Find Extra One

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟. 看看Y左边或右边的点个数是否<=1 [代码] #include <bits/stdc++.h> using ...

  4. 【例题 7-2 UVA - 11059】Maximum Product

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] C语言for循环练习题 [代码] /* 1.Shoud it use long long ? 2.Have you ever tes ...

  5. 【Codeforces Round #447 (Div. 2) C】Marco and GCD Sequence

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把gcd(a[1..n])放在输入的n个数之间. [代码] /* 1.Shoud it use long long ? 2.Have ...

  6. Mycat 读写分离+分库分表

    上次进过GTID复制的学习记录,已经搭建好了主从复制的服务器,现在利用现有的主从复制环境,加上正在研究的Mycat,实现了主流分布式数据库的测试 Mycat就不用多介绍了,可以实现很多分布式数据库的功 ...

  7. Leetcode 第 2 题(Add Two Numbers)

    Leetcode 第 2 题(Add Two Numbers) 题目例如以下: Question You are given two linked lists representing two non ...

  8. 关于LWIP断开网线后重连问题(热插拔问题)

    近期在弄STM32+LWIP协议.在网络拔掉网线情况下.无法又一次连接. 网上找了好多方法都没有实现,着实郁闷! 后来无意间看到了临时解决这一问题的方法.尽管不是那么完美,但最算能解决这个问题.分享给 ...

  9. Altium Designer绘制mark点

    mark注:我之前是按照下面的文章去制作的,由于头一次制作没有经验,不是很成功 文章是正确的 只是我的一些配置出错了: 先看一下我们的板子: 关于错误mark点,主要是周边又一圈亮锡,, 原因大概是敷 ...

  10. [RxJS] Multicasting shortcuts: publish() and variants

    Because using multicast with a new Subject is such a common pattern, there is a shortcut in RxJS for ...