导读:Android中的四大组件是 Activity、Service、Broadcast和Content Provider。而Intent是一个对动作和行为的抽象描述,负责组件之间程序之间进行消息传递。
Broadcast Receiver 的使用
1、Broadcast Receiver简介
2、Broadcast Receiver接收系统自带的广播
3、自定义广播
Broadcast Receiver接收系统自带的广播
我们做一个例子,功能是在系统启动时播放一首音乐。
1、建立一个项目Lesson21_BroadcastReceiver,拷贝一首音乐进res/raw目录
2、建立HelloBroadcastReceiver.java 内容如下:
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();
|
在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" >
|
06 | <action android:name= "android.intent.action.MAIN" >
|
07 | <category android:name= "android.intent.category.LAUNCHER" >
|
08 | </category></action></intent>
|
10 | <!-- 定义Broadcast Receiver 指定监听的Action -->
|
11 | <receiver android:name= "HelloBroadReciever" >
|
13 | <action android:name= "android.intent.action.BOOT_COMPLETED" >
|
16 | </application></manifest> |
自定义广播 下面我们学习自己制作一个广播。我们接着刚才的例子,继续写下去。 4、在MainBroadcastReceiver.java中填写如下代码:
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" );
|
更改 HelloBroadReceiver.java 内容如下:
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();
|
更改 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" >
|
06 | <action android:name= "android.intent.action.MAIN" >
|
07 | <category android:name= "android.intent.category.LAUNCHER" >
|
08 | </category></action></intent>
|
10 | <!-- 定义Broadcast Receiver 指定监听的Action 这里我们的接收器,接收了 2 个Action,一个系统的一个我们自定义的 -->
|
11 | <receiver android:name= "HelloBroadReciever" >
|
13 | <action android:name= "android.intent.action.BOOT_COMPLETED" >
|
16 | <action android:name= "android.basic.lesson21.HelloYaoYao" >
|
21 | <uses -sdk= ""
android:minsdkversion= "8" >
|