导读: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 内容如下:

01 package android.demo;
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 }
在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、在MainBroadcastReceiver.java中填写如下代码:
01 package android.demo;
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 }
更改 HelloBroadReceiver.java 内容如下:
01 package android.demo;
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 }
更改 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>

更多相关文章

  1. 【Android(安卓)Developers Training】 76. 用Wi-Fi创建P2P连接
  2. Pro Android学习笔记(九七):BroadcastReceiver(1):基础小例子
  3. Android电量监控
  4. Android(安卓)串口通信之间的发送数据与接收数据(详解)
  5. Android(安卓)实现音乐播放器【源码+注释】— MediaPlayer
  6. 上官网学android之八(Interacting with Other Apps)
  7. Android中的四大组件
  8. Android(安卓)Intent 其中一个分析
  9. 【Android】监听蓝牙状态变化

随机推荐

  1. Android(安卓)ContentProvider的使用
  2. Android(安卓)RadioButton 文字在左边
  3. Android(安卓)数据存储与读取:SQLite
  4. GCM(5)Working with MAVEN and Android(
  5. Android(安卓)layout xml总结
  6. android几种布局
  7. Android中android:visibility的3中属性的
  8. Android多媒体播放器源码解析(stagefrigh
  9. Android(安卓)TextUtils类介绍
  10. Android(安卓)实现json网络数据通过BaseA