第二十一讲:Broadcast Receiver 使用入门
2013-07-12 09:55
501 查看
本讲内容:BroadcastReceiver的使用
1、BroadcastReceiver简介
2、BroadcastReceiver接收系统自带的广播
3、自定义广播
一、BroadcastReceiver简介
Android中的四大组件是Activity、Service、Broadcast和ContentProvider。而Intent是一个对动作和行为的抽象描述,负责组件之间程序之间进行消息传递。那么BroadcastReceiver组件就提供了一种把Intent作为一个消息广播出去,由所有对其感兴趣的程序对其作出反应的机制。
二、BroadcastReceiver接收系统自带的广播
我们做一个例子,功能是在系统启动时播放一首音乐。
1、建立一个项目Lesson21_BroadcastReceiver,拷贝一首音乐进res/raw目录
2、建立HelloBroadcastReceiver.java内容如下:
viewsource
print?
3、在AndroidManifest.xml中注册此Receiver:
viewsource
print?
4、发布程序,启动模拟器,可以在Logcat中看到:
同时能听到音乐播放的声音。说明我们确实接收到了系统启动的广播事件,并做出了响应。
三、自定义广播
下面我们学习自己制作一个广播。我们接着刚才的例子,继续写下去。
5、在MainBroadcastReceiver.java中填写如下代码:
viewsource
print?
6、更改HelloBroadReceiver.java内容如下:
viewsource
print?
7、更改AndroidManifest.xml内容如下:
viewsource
print?
8、运行程序,点击按钮,查看LogCat,听听声音
好了,本讲就到这里。
1、BroadcastReceiver简介
2、BroadcastReceiver接收系统自带的广播
3、自定义广播
一、BroadcastReceiver简介
Android中的四大组件是Activity、Service、Broadcast和ContentProvider。而Intent是一个对动作和行为的抽象描述,负责组件之间程序之间进行消息传递。那么BroadcastReceiver组件就提供了一种把Intent作为一个消息广播出去,由所有对其感兴趣的程序对其作出反应的机制。
二、BroadcastReceiver接收系统自带的广播
我们做一个例子,功能是在系统启动时播放一首音乐。
1、建立一个项目Lesson21_BroadcastReceiver,拷贝一首音乐进res/raw目录
2、建立HelloBroadcastReceiver.java内容如下:
01 | package android.basic.lesson21; |
02 |
03 | import
|
04 | import
|
05 | import
|
06 | import
|
07 | import
|
08 |
09 | public class HelloBroadReciever extends BroadcastReceiver{ |
10 |
11 | //如果接收的事件发生 |
12 | @Override |
13 | public void onReceive(Contextcontext,Intentintent){ |
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 | } |
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < MANIFEST package = "android.basic.lesson21" android:versioncode = "1" android:versionname = "1.0" xmlns:android = " > |
03 | < APPLICATION android:label = "@string/app_name" android:icon = "@drawable/icon" > |
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 | </ INTENT > |
09 | </ ACTIVITY > |
10 | <!--定义BroadcastReceiver指定监听的Action--> |
11 | < RECEIVER android:name = "HelloBroadReciever" > |
12 | < INTENT -filter> |
13 | < ACTION android:name = "android.intent.action.BOOT_COMPLETED" /> |
14 | </ INTENT > |
15 | </ RECEIVER > |
16 | </ APPLICATION ></ MANIFEST > |
同时能听到音乐播放的声音。说明我们确实接收到了系统启动的广播事件,并做出了响应。
三、自定义广播
下面我们学习自己制作一个广播。我们接着刚才的例子,继续写下去。
5、在MainBroadcastReceiver.java中填写如下代码:
01 | package android.basic.lesson21; |
02 |
03 | import
|
04 | import
|
05 | import
|
06 | import
|
07 | import
|
08 |
09 | public class MainBroadcastReceiver extends Activity{ |
10 | /**Calledwhentheactivityisfirstcreated.*/ |
11 | @Override |
12 | public void onCreate(BundlesavedInstanceState){ |
13 | super .onCreate(savedInstanceState); |
14 | setContentView(R.layout.main); |
15 |
16 | Buttonb1=(Button)findViewById(R.id.Button01); |
17 |
18 | b1.setOnClickListener( new View.OnClickListener(){ |
19 |
20 | @Override |
21 | public void onClick(Viewv){ |
22 | //定义一个intent |
23 | Intentintent= new Intent().setAction( |
24 | "android.basic.lesson21.Hello" ).putExtra( "yaoyao" , |
25 | "yaoyaois189daysold,27weeks--2010-08-10" ); |
26 | //广播出去 |
27 | sendBroadcast(intent); |
28 | } |
29 | }); |
30 | } |
31 | } |
01 | package android.basic.lesson21; |
02 |
03 | import
|
04 | import
|
05 | import
|
06 | import
|
07 | import
|
08 |
09 | public class HelloBroadReciever extends BroadcastReceiver{ |
10 |
11 | //如果接收的事件发生 |
12 | @Override |
13 | public void onReceive(Contextcontext,Intentintent){ |
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" , "SayHellotoYaoyao!!!!!!!!!!!!!!!!!!!!!!!!!" ); |
21 | Log.e( "HelloBroadReciever" ,intent.getStringExtra( "yaoyao" )); |
22 | } |
23 |
24 | //播放一首音乐 |
25 | MediaPlayer.create(context,R.raw.babayetu).start(); |
26 | } |
27 | } |
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < MANIFEST package = "android.basic.lesson21" android:versioncode = "1" android:versionname = "1.0" xmlns:android = " > |
03 | < APPLICATION android:label = "@string/app_name" android:icon = "@drawable/icon" > |
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 | </ INTENT > |
09 | </ ACTIVITY > |
10 | <!--定义BroadcastReceiver指定监听的Action这里我们的接收器,接收了2个Action,一个系统的一个我们自定义的--> |
11 | < RECEIVER android:name = "HelloBroadReciever" > |
12 | < INTENT -filter> |
13 | < ACTION android:name = "android.intent.action.BOOT_COMPLETED" /> |
14 | </ INTENT > |
15 | < INTENT -filter> |
16 | < ACTION android:name = "android.basic.lesson21.HelloYaoYao" /> |
17 | </ INTENT > |
18 |
19 | </ RECEIVER > |
20 | </ APPLICATION > |
21 | < USES android:minsdkversion = "8" -sdk/> |
22 | </ MANIFEST > |
好了,本讲就到这里。
相关文章推荐
- 第二十一讲:Broadcast Receiver 使用入门
- Broadcast Receiver 使用入门
- Android Broadcast receiver使用入门
- 某APK中使用了动态注册BroadcastReceiver,Launcher中动态加载此APK出现java.lang.SecurityException异常的解决方法
- Android之BroadcastReceiver的使用
- 关于BroadcastReceiver的学习与使用
- BroadcastReceiver使用方式
- Android BroadcastReceiver在Fragment中的使用
- 使用广播接受者BroadcastReceiver监测网络变化
- Android BroadcastReceiver之快速上手使用广播
- Android中BroadCastReceiver使用
- 使用Service和BroadcastReceiver实时监听网络状态
- 使用Service+BroadcastReceiver实现定时更新天气
- BroadcastReceiver中使用自定义Permission
- android BroadcastReceiver 使用小结
- Android中广播接收者BroadcastReceiver的理解与基本使用
- Android之BroadcastReceiver(四):使用本地广播
- Android中BroadCastReceiver使用
- Android之BroadcastReceiver的使用
- BroadCastReceiver概念与使用