导读: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" ); |
27 | sendBroadcast(intent); |
更改 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" > |
- 【Android(安卓)Developers Training】 76. 用Wi-Fi创建P2P连接
- Pro Android学习笔记(九七):BroadcastReceiver(1):基础小例子
- Android电量监控
- Android(安卓)串口通信之间的发送数据与接收数据(详解)
- Android(安卓)实现音乐播放器【源码+注释】— MediaPlayer
- 上官网学android之八(Interacting with Other Apps)
- Android中的四大组件
- Android(安卓)Intent 其中一个分析
- 【Android】监听蓝牙状态变化
随机推荐
-
Android(安卓)ContentProvider的使用
-
Android(安卓)RadioButton 文字在左边
-
Android(安卓)数据存储与读取:SQLite
-
GCM(5)Working with MAVEN and Android(
-
Android(安卓)layout xml总结
-
android几种布局
-
Android中android:visibility的3中属性的
-
Android多媒体播放器源码解析(stagefrigh
-
Android(安卓)TextUtils类介绍
-
Android(安卓)实现json网络数据通过BaseA