本讲内容: Broadcast Receiver 的使用
1、Broadcast Receiver简介
2、Broadcast Receiver接收系统自带的广播
3、自定义广播
一、Broadcast Receiver简介
Android中的四大组件是 Activity、Service、Broadcast和Content Provider。而Intent是一个对动作和行为的抽象描述,负责组件之间程序之间进行消息传递。那么Broadcast Receiver组件就提供了一种把Intent作为一个消息广播出去,由所有对其感兴趣的程序对其作出反应的机制。
二、Broadcast Receiver接收系统自带的广播
我们做一个例子,功能是在系统启动时播放一首音乐。
1、建立一个项目Lesson21_BroadcastReceiver,拷贝一首音乐进res/raw目录
2、建立HelloBroadcastReceiver.java 内容如下:
01 | package android.basic.lesson21; |
03 | import android.content.BroadcastReceiver; |
04 | import android.content.Context; |
05 | import android.content.Intent; |
06 | import android.media.MediaPlayer; |
07 | import android.util.Log; |
09 | public class HelloBroadReciever extends BroadcastReceiver { |
13 | public void onReceive(Context context, Intent intent) { |
15 | Log.e( "HelloBroadReciever" , "BOOT_COMPLETED!!!!!!!!!!!!!!!!!!!!!!!!!" ); |
16 | Log.e( "HelloBroadReciever" , "" +intent.getAction()); |
19 | MediaPlayer.create(context, R.raw.babayetu).start(); |
3、在AndroidManifest.xml中注册此Receiver :
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
03 | < APPLICATION android:label = "@string/app_name" android:icon = "@drawable/icon" > |
04 | < ACTIVITY android:label = "@string/app_name" android:name = ".MainBroadcastReceiver" > |
06 | < ACTION android:name = "android.intent.action.MAIN" /> |
07 | < CATEGORY android:name = "android.intent.category.LAUNCHER" /> |
11 | < RECEIVER android:name = "HelloBroadReciever" > |
13 | < ACTION android:name = "android.intent.action.BOOT_COMPLETED" /> |
16 | </ APPLICATION ></ MANIFEST > |
4、发布程序,启动模拟器,可以在Logcat中看到:
![~OY1$Y7O3}O64K{UCN_@$79 ~OY1$Y7O3}O64K{UCN_@$79](http://android.yaohuiji.com/wp-content/uploads/2010/08/OY1Y7O3O64KUCN_79_thumb.jpg)
同时能听到音乐播放的声音。说明我们确实接收到了系统启动的广播事件,并做出了响应。
三、自定义广播
下面我们学习自己制作一个广播。我们接着刚才的例子,继续写下去。
5、在MainBroadcastReceiver.java中填写如下代码:
01 | package android.basic.lesson21; |
03 | import android.app.Activity; |
04 | import android.content.Intent; |
05 | import android.os.Bundle; |
06 | import android.view.View; |
07 | import android.widget.Button; |
09 | public class MainBroadcastReceiver extends Activity { |
10 | /** Called when the activity is first created. */ |
12 | public void onCreate(Bundle savedInstanceState) { |
13 | super .onCreate(savedInstanceState); |
14 | setContentView(R.layout.main); |
16 | Button b1 = (Button) findViewById(R.id.Button01); |
18 | b1.setOnClickListener( new View.OnClickListener() { |
21 | public void onClick(View v) { |
23 | Intent intent = new Intent().setAction( |
24 | "android.basic.lesson21.Hello" ).putExtra( "yaoyao" , |
25 | "yaoyao is 189 days old ,27 weeks -- 2010-08-10" ); |
27 | sendBroadcast(intent); |
6、更改 HelloBroadReceiver.java 内容如下:
01 | package android.basic.lesson21; |
03 | import android.content.BroadcastReceiver; |
04 | import android.content.Context; |
05 | import android.content.Intent; |
06 | import android.media.MediaPlayer; |
07 | import android.util.Log; |
09 | public class HelloBroadReciever extends BroadcastReceiver { |
13 | public void onReceive(Context context, Intent intent) { |
15 | if (intent.getAction().equals( "android.intent.action.BOOT_COMPLETED" )){ |
16 | Log.e( "HelloBroadReciever" , "BOOT_COMPLETED !!!!!!!!!!!!!!!!!!!!!!!!!" ); |
19 | if (intent.getAction().equals( "android.basic.lesson21.Hello" )){ |
20 | Log.e( "HelloBroadReciever" , "Say Hello to Yaoyao !!!!!!!!!!!!!!!!!!!!!!!!!" ); |
21 | Log.e( "HelloBroadReciever" , intent.getStringExtra( "yaoyao" )); |
25 | MediaPlayer.create(context, R.raw.babayetu).start(); |
7、更改 AndroidManifest.xml 内容如下:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
03 | < APPLICATION android:label = "@string/app_name" android:icon = "@drawable/icon" > |
04 | < ACTIVITY android:label = "@string/app_name" android:name = ".MainBroadcastReceiver" > |
06 | < ACTION android:name = "android.intent.action.MAIN" /> |
07 | < CATEGORY android:name = "android.intent.category.LAUNCHER" /> |
11 | < RECEIVER android:name = "HelloBroadReciever" > |
13 | < ACTION android:name = "android.intent.action.BOOT_COMPLETED" /> |
16 | < ACTION android:name = "android.basic.lesson21.HelloYaoYao" /> |
21 | < USES android:minsdkversion = "8" -sdk /> |
8、运行程序,点击按钮,查看LogCat,听听声音
![3@({Q[{{(E1EA7UQTZ[TDGK 3@({Q[{{(E1EA7UQTZ[TDGK](http://android.yaohuiji.com/wp-content/uploads/2010/08/3QE1EA7UQTZTDGK_thumb.jpg)
好了,本讲就到这里。