一、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;
02
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;
08
09 public class HelloBroadReciever extends BroadcastReceiver {
10
11 //如果接收的事件发生
12 @Override
13 public void onReceive(Context context, Intent intent) {
14 //则输出日志
15 Log.e("HelloBroadReciever", "BOOT_COMPLETED!!!!!!!!!!!!!!!!!!!!!!!!!");
16 Log.e("HelloBroadReciever", ""+intent.getAction());
17
18 //则播放一首音乐
19 MediaPlayer.create(context, R.raw.babayetu).start();
20 }
21 }
3、在AndroidManifest.xml中注册此Receiver :
01 <?xml version="1.0" encoding="utf-8"?>
02 <manifest xmlns:android="http://schemas.android.com/apk/res/android"android:versionname="1.0" android:versioncode="1" package="android.basic.lesson21">
03 <application android:icon="@drawable/icon" android:label="@string/app_name">
04 <activity android:label="@string/app_name"android:name=".MainBroadcastReceiver">
05 <intent -filter="">
06 <action android:name="android.intent.action.MAIN">
07 <category android:name="android.intent.category.LAUNCHER">
08 </category></action></intent>
09 </activity>
10 <!-- 定义Broadcast Receiver 指定监听的Action -->
11 <receiver android:name="HelloBroadReciever">
12 <intent -filter="">
13 <action android:name="android.intent.action.BOOT_COMPLETED">
14 </action></intent>
15 </receiver>
16 </application></manifest>
4、发布程序,启动模拟器,可以在Logcat中看到:
[img]http://dl.iteye.com/upload/attachment/446465/2689a14d-b4b7-32f7-a812-f12fec4059cf.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/446467/119cfa4a-f49e-3911-8258-34193b06d792.png[/img]
同时能听到音乐播放的声音。说明我们确实接收到了系统启动的广播事件,并做出了响应。
三、自定义广播
下面我们学习自己制作一个广播。我们接着刚才的例子,继续写下去。
5、在MainBroadcastReceiver.java中填写如下代码:
01 package android.basic.lesson21;
02
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;
08
09 public class MainBroadcastReceiver extends Activity {
10 /** Called when the activity is first created. */
11 @Override
12 public void onCreate(Bundle savedInstanceState) {
13 super.onCreate(savedInstanceState);
14 setContentView(R.layout.main);
15
16 Button b1 = (Button) findViewById(R.id.Button01);
17
18 b1.setOnClickListener(new View.OnClickListener() {
19
20 @Override
21 public void onClick(View v) {
22 //定义一个intent
23 Intent intent = new Intent().setAction(
24 "android.basic.lesson21.Hello").putExtra("yaoyao",
25 "yaoyao is 189 days old ,27 weeks -- 2010-08-10");
26 //广播出去
27 sendBroadcast(intent);
28 }
29 });
30 }
31 }
6、更改 HelloBroadReceiver.java 内容如下:
01 package android.basic.lesson21;
02
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;
08
09 public class HelloBroadReciever extends BroadcastReceiver {
10
11 //如果接收的事件发生
12 @Override
13 public void onReceive(Context context, Intent intent) {
14 //对比Action决定输出什么信息
15 if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
16 Log.e("HelloBroadReciever", "BOOT_COMPLETED !!!!!!!!!!!!!!!!!!!!!!!!!");
17 }
18
19 if(intent.getAction().equals("android.basic.lesson21.Hello")){
20 Log.e("HelloBroadReciever", "Say Hello to Yaoyao !!!!!!!!!!!!!!!!!!!!!!!!!");
21 Log.e("HelloBroadReciever", intent.getStringExtra("yaoyao"));
22 }
23
24 //播放一首音乐
25 MediaPlayer.create(context, R.raw.babayetu).start();
26 }
27 }
7、更改 AndroidManifest.xml 内容如下:
01 <?xml version="1.0" encoding="utf-8"?>
02 <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="android.basic.lesson21" android:versionname="1.0" android:versioncode="1">
03 <application android:icon="@drawable/icon" android:label="@string/app_name">
04 <activity android:label="@string/app_name"android:name=".MainBroadcastReceiver">
05 <intent -filter="">
06 <action android:name="android.intent.action.MAIN">
07 <category android:name="android.intent.category.LAUNCHER">
08 </category></action></intent>
09 </activity>
10 <!-- 定义Broadcast Receiver 指定监听的Action 这里我们的接收器,接收了2个Action,一个系统的一个我们自定义的 -->
11 <receiver android:name="HelloBroadReciever">
12 <intent -filter="">
13 <action android:name="android.intent.action.BOOT_COMPLETED">
14 </action></intent>
15 <intent -filter="">
16 <action android:name="android.basic.lesson21.HelloYaoYao">
17 </action></intent>
18
19 </receiver>
20 </application>
21 <uses -sdk="" android:minsdkversion="8">
22 </uses></manifest>
8、运行程序,点击按钮,查看LogCat,听听声音
[img]http://dl.iteye.com/upload/attachment/446468/649b1ae9-4f84-3d6d-92a7-6435c4d278bf.png[/img]
[img]http://dl.iteye.com/upload/attachment/446471/bf812fc0-b4c4-3323-878a-6d05d8cdfe29.jpg[/img]
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;
02
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;
08
09 public class HelloBroadReciever extends BroadcastReceiver {
10
11 //如果接收的事件发生
12 @Override
13 public void onReceive(Context context, Intent intent) {
14 //则输出日志
15 Log.e("HelloBroadReciever", "BOOT_COMPLETED!!!!!!!!!!!!!!!!!!!!!!!!!");
16 Log.e("HelloBroadReciever", ""+intent.getAction());
17
18 //则播放一首音乐
19 MediaPlayer.create(context, R.raw.babayetu).start();
20 }
21 }
3、在AndroidManifest.xml中注册此Receiver :
01 <?xml version="1.0" encoding="utf-8"?>
02 <manifest xmlns:android="http://schemas.android.com/apk/res/android"android:versionname="1.0" android:versioncode="1" package="android.basic.lesson21">
03 <application android:icon="@drawable/icon" android:label="@string/app_name">
04 <activity android:label="@string/app_name"android:name=".MainBroadcastReceiver">
05 <intent -filter="">
06 <action android:name="android.intent.action.MAIN">
07 <category android:name="android.intent.category.LAUNCHER">
08 </category></action></intent>
09 </activity>
10 <!-- 定义Broadcast Receiver 指定监听的Action -->
11 <receiver android:name="HelloBroadReciever">
12 <intent -filter="">
13 <action android:name="android.intent.action.BOOT_COMPLETED">
14 </action></intent>
15 </receiver>
16 </application></manifest>
4、发布程序,启动模拟器,可以在Logcat中看到:
[img]http://dl.iteye.com/upload/attachment/446465/2689a14d-b4b7-32f7-a812-f12fec4059cf.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/446467/119cfa4a-f49e-3911-8258-34193b06d792.png[/img]
同时能听到音乐播放的声音。说明我们确实接收到了系统启动的广播事件,并做出了响应。
三、自定义广播
下面我们学习自己制作一个广播。我们接着刚才的例子,继续写下去。
5、在MainBroadcastReceiver.java中填写如下代码:
01 package android.basic.lesson21;
02
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;
08
09 public class MainBroadcastReceiver extends Activity {
10 /** Called when the activity is first created. */
11 @Override
12 public void onCreate(Bundle savedInstanceState) {
13 super.onCreate(savedInstanceState);
14 setContentView(R.layout.main);
15
16 Button b1 = (Button) findViewById(R.id.Button01);
17
18 b1.setOnClickListener(new View.OnClickListener() {
19
20 @Override
21 public void onClick(View v) {
22 //定义一个intent
23 Intent intent = new Intent().setAction(
24 "android.basic.lesson21.Hello").putExtra("yaoyao",
25 "yaoyao is 189 days old ,27 weeks -- 2010-08-10");
26 //广播出去
27 sendBroadcast(intent);
28 }
29 });
30 }
31 }
6、更改 HelloBroadReceiver.java 内容如下:
01 package android.basic.lesson21;
02
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;
08
09 public class HelloBroadReciever extends BroadcastReceiver {
10
11 //如果接收的事件发生
12 @Override
13 public void onReceive(Context context, Intent intent) {
14 //对比Action决定输出什么信息
15 if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
16 Log.e("HelloBroadReciever", "BOOT_COMPLETED !!!!!!!!!!!!!!!!!!!!!!!!!");
17 }
18
19 if(intent.getAction().equals("android.basic.lesson21.Hello")){
20 Log.e("HelloBroadReciever", "Say Hello to Yaoyao !!!!!!!!!!!!!!!!!!!!!!!!!");
21 Log.e("HelloBroadReciever", intent.getStringExtra("yaoyao"));
22 }
23
24 //播放一首音乐
25 MediaPlayer.create(context, R.raw.babayetu).start();
26 }
27 }
7、更改 AndroidManifest.xml 内容如下:
01 <?xml version="1.0" encoding="utf-8"?>
02 <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="android.basic.lesson21" android:versionname="1.0" android:versioncode="1">
03 <application android:icon="@drawable/icon" android:label="@string/app_name">
04 <activity android:label="@string/app_name"android:name=".MainBroadcastReceiver">
05 <intent -filter="">
06 <action android:name="android.intent.action.MAIN">
07 <category android:name="android.intent.category.LAUNCHER">
08 </category></action></intent>
09 </activity>
10 <!-- 定义Broadcast Receiver 指定监听的Action 这里我们的接收器,接收了2个Action,一个系统的一个我们自定义的 -->
11 <receiver android:name="HelloBroadReciever">
12 <intent -filter="">
13 <action android:name="android.intent.action.BOOT_COMPLETED">
14 </action></intent>
15 <intent -filter="">
16 <action android:name="android.basic.lesson21.HelloYaoYao">
17 </action></intent>
18
19 </receiver>
20 </application>
21 <uses -sdk="" android:minsdkversion="8">
22 </uses></manifest>
8、运行程序,点击按钮,查看LogCat,听听声音
[img]http://dl.iteye.com/upload/attachment/446468/649b1ae9-4f84-3d6d-92a7-6435c4d278bf.png[/img]
[img]http://dl.iteye.com/upload/attachment/446471/bf812fc0-b4c4-3323-878a-6d05d8cdfe29.jpg[/img]