Android应用开发学习笔记之BroadcastReceiver
作者:刘昊昱
博客:http://blog.csdn.net/liuhaoyutz
一、BroadcastReceiver机制概述
Broadcast Receiver是Android的一种“广播发布/消息接收”机制,或者说的更准确一些,是一种“监听”机制。作为广播发布者的应用程序,可以在不知道谁(如果有的话)将接收这个广播消息的情况下发出一个广播消息(广播的消息实际上就是一个Intent对象)。而消息接收者可以指定自己将接收哪些消息(通过使用intent-filter),如果出现了他指定的消息,消息接收者就会被调用对消息进行处理。
要发出一个广播消息,可以创建一个Intent对象,并调用sendBroadcast()方法将Intent对象做为消息广播出去。
广播消息的接收是通过继承BroadcastReceiver类来实现的,我们需要实现onReceive()函数,在该函数中完成对消息(即Intent对象)的处理。
二、自定义广播消息
下面来看一个例子程序,该程序演示怎样自定义一个广播消息并发送出去,同时也演示了怎样接收指定消息并处理,该程序运行效果如下:
先来看主布局文件,其内容如下:
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="@string/hello" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:onClick="onButtonClick"
android:textSize="20dp"
android:text="发送消息" /> </LinearLayout>
下面来看主Activity文件,其内容如下:
package com.liuhaoyu; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View; public class MainActivity extendsActivity {
privatestatic final String MY_ACTION="com.liuhaoyu.broastcast.action.MY_ACTION"; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} public void onButtonClick(View v) {
Intent intent=newIntent();
intent.setAction(MY_ACTION);
intent.putExtra("msg","Broadcast Receiver测试");
sendBroadcast(intent);
}
}
当点击按钮时,创建一个广播消息(即Intent对象),指定该消息对应的Action是MY_ACTION,通过调用sendBroadcast()函数将消息广播出去。
下面来看该程序自定义的BraodcastReceiver:
package com.liuhaoyu; importandroid.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; public class BroadcastReceiverTestextends BroadcastReceiver { @Override
publicvoid onReceive(Context arg0, Intent arg1) {
//TODO Auto-generated method stub
Stringmsg=arg1.getStringExtra("msg");
Toast.makeText(arg0,msg, Toast.LENGTH_LONG).show();
} }
可以看到,我们继承了BroadcastReceiver类,并实现了onReceiver()函数,该函数中对消息的处理就是弹出一个消息提示。
下面程序在AndroidManifest.xml文件中声明BroadcastReceiverTest,并通过intent-filter指定监听MY_ACTION消息:
<receiver android:name=".BroadcastReceiverTest">
<intent-filter>
<action android:name="com.liuhaoyu.broastcast.action.MY_ACTION"/>
</intent-filter>
</receiver>
三、接收系统预定义广播消息
Android系统提供了一些预定义的广播消息,Android官方文档上关于消息(即Intent对象)对应的Action描述如下:
These are the currentstandard actions that Intent defines for receiving broadcasts (usually through registerReceiver(BroadcastReceiver,IntentFilter) ora <receiver> tag in a manifest).
下面看一个接收系统预定义的BOOT_COMPLETE广播消息的例子,当Android启动起来后,会广播BOOT_COMPLETE消息,我们的应用程序声明监听该消息,所以每次Android启动起来后,我们的应用程序就会做出对该消息的响应,怎样响应就是我们在onReceiver()函数中实现的,这个程序的响应是播放一段音乐,弹出一个提示信息,并打印LOG信息。该程序运行效果如下:
下面是LogCat中显示的LOG信息:
我们先来看主布局文件:
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" /> </LinearLayout>
没有什么特殊的,就是系统自动生成的,我们不做任何改变。
下面看主Activity文件,其内容如下:
package com.liuhaoyu; import android.app.Activity;
import android.os.Bundle; public class MainActivity extendsActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
也是系统自动生成的,不做任何改变。
下面就是重点了,即我们的BroadcastReceiver的实现:
package com.liuhaoyu; importandroid.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.util.Log;
import android.widget.Toast; public class BootReceiver extendsBroadcastReceiver {
@Override
publicvoid onReceive(Context context, Intent intent) {
Log.e("BootReceiver","received BOOT_COMPLETED intent!");
MediaPlayer.create(context,R.raw.enter).start();
Toast.makeText(context,"BroadcastReceiver: " + intent.getAction(),Toast.LENGTH_SHORT).show();
}
}
可以看到,我们继承了BraodcastReceiver类,实现了onReceiver()函数,在该函数中,打印一条LOG信息,播放一段音乐,并弹出一个提示信息,这就是我们的应用程序接收到指定广播消息后的处理。
有了BroadcastReceiver,还有一个重要的任务是声明我们的应用程序监听哪些消息,这个工作是在AndroidManifest.xml文件中完成的,在系统默认内容之外,添加了两处内容:
一是声明BroadcastReceiver,同时通过intent-filter指定监听BOOT_COMPLETED消息:
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
二是指定权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Android应用开发学习笔记之BroadcastReceiver的更多相关文章
- Android应用开发学习笔记之播放音频
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android支持常用音视频格式文件的播放,本文我们来学习怎样开发Android应用程序对音视频进行操作. Andr ...
- android移动开发学习笔记(二)神奇的Web API
本次分两个大方向去讲解Web Api,1.如何实现Web Api?2.如何Android端如何调用Web Api?对于Web Api是什么?有什么优缺点?为什么用WebApi而不用Webservice ...
- Android应用开发学习笔记之事件处理
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android提供的事件处理机制分为两类:一是基于监听的事件处理:二是基于回调的事件处理.对于基于监听的事件处理,主 ...
- Android应用开发学习笔记之Intent
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Intent是什么呢?来看Android官网上的定义: An intent is an abstractdescri ...
- Android应用开发学习笔记之AsyncTask
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 在上一篇文章中我们学习了多线程和Handler消息处理机制,如果有计算量比较大的任务,可以创建一个新线程执行计算工作 ...
- [Android游戏开发学习笔记]View和SurfaceView
本文为阅读http://blog.csdn.net/xiaominghimi/article/details/6089594的笔记. 在Android游戏中充当主要角色的,除了控制类就是显示类.而在A ...
- Android应用开发学习笔记之Fragment
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Fragment翻译成中文就是“碎片”.“片断”的意思,Fragment通常用来作为一个Activity用户界面的一 ...
- Android应用开发学习笔记之菜单
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android中的菜单分为选项菜单(OptionMenu)和上下文菜单(Context Menu).通常使用菜单资源 ...
- Android应用开发学习笔记之多线程与Handler消息处理机制
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 和JAVA一样,Android下我们可以通过创建一个Thread对象实现多线程.Thread类有多个构造函数,一般通 ...
随机推荐
- QComboBox 添加图片(自带addItem函数就有这个功能,从没有注意过)
方法: 使用 QComboxBox::addItem(QIcon, QString); 示例: 点击(此处)折叠或打开 QComboBox *combo_status = new QComboB ...
- GQ_百度百科
GQ_百度百科 <GQ>杂志(原名<Gentlemen's Quarterly>,中文名<智族>)是一本男性月刊,内容著重于男性的时尚.风格.文化,也包括美食.电影 ...
- SQL Server SQL高级查询语句小结(转)
--select select * from student; --all 查询所有 select all sex from student; --distinct 过滤重复 select disti ...
- Oracle笔记之对象权限与系统权限总结
对象权限与系统权限 创建表和创建session是系统权限: 系统管理员是有权限去訪问其它表的 以sys登录 sqlplus sys/on_change_install as sysdba; 创 ...
- 淘宝PK京东:哥刷的不是广告,刷的是存在
冯强/文 (昨晚看阿根廷vs瑞士时手机上敲的,看完太激动忘发了,现配了图发上来) 这两天,关于京东.淘宝渠道下沉的新闻中,两家略带喜感的农村墙体广告在互联网上传播,例如以下图: 京东这图片,越看越像P ...
- 毕业论文endnote使用
毕业论文的参考文献的标准格式,参考了一下文章 http://blog.sina.com.cn/s/blog_62a1f43801016fq5.html http://blog.csdn.net/zhe ...
- iOS开发之视图控制器(UIViewController)
视图控制器应该在MVC设计模式中扮演控制层(C)的角色,UIViewController的职责对内管理与之关联的View,对外跟其他UIViewController通信和协调.一个视图控制器管理一个视 ...
- (Problem 2)Even Fibonacci numbers
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting w ...
- 【转】介绍几个图论和复杂网络的程序库 —— BGL,QuickGraph,igraph和NetworkX
原文来自:http://blog.sciencenet.cn/blog-404069-297233.html 作复杂网络研究离不开对各种实际或模拟网络的统计.计算.绘图等工作.对于一般性的工作,我们可 ...
- Week6(10月14日)
Part I:提问 =========================== 1.什么是视图模型?2.我们在留言本中,加入了一个怎样的视图模型?如何处理它? Part II:Ch05 视图模型 === ...