Broadcast Receiver
的使用
1、Broadcast
Receiver简介
2、Broadcast
Receiver接收系统自带的广播
3、自定义广播
一、Broadcast Receiver简介
Android中的四大组件是 Activity、Service、Broadcast和Content
Provider。而Intent是一个对动作和行为的抽象描述,负责组件之间程序之间进行消息传递。那么Broadcast
Receiver组件就提供了一种把Intent作为一个消息广播出去,由所有对其感兴趣的程序对其作出反应的机制。
二、Broadcast Receiver接收系统自带的广播
我们做一个例子,功能是在系统启动时播放一首音乐。
1、建立一个项目Lesson21_BroadcastReceiver,拷贝一首音乐进res/raw目录
2、建立HelloBroadcastReceiver.java
内容如下:
java代码:
- package android.demo;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.util.Log;
- public class HelloBroadReciever extends BroadcastReceiver {
- //如果接收的事件发生
- @Override
- public void onReceive(Context context, Intent intent) {
- //则输出日志
- Log.e("HelloBroadReciever", "BOOT_COMPLETED!!!!!!!!!!!!!!!!!!!!!!!!!");
- Log.e("HelloBroadReciever", ""+intent.getAction());
- //则播放一首音乐
- MediaPlayer.create(context, R.raw.babayetu).start();
- }
- }
3、在AndroidManifest.xml中注册此Receiver
:
java代码:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionname="1.0" android:versioncode="1" package="android.basic.lesson21">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:label="@string/app_name" android:name=".MainBroadcastReceiver">
- <intent -filter="">
- <action android:name="android.intent.action.MAIN">
- <category android:name="android.intent.category.LAUNCHER">
- </category></action></intent>
- </activity>
- <!-- 定义Broadcast Receiver 指定监听的Action -->
- <receiver android:name="HelloBroadReciever">
- <intent -filter="">
- <action android:name="android.intent.action.BOOT_COMPLETED">
- </action></intent>
- </receiver>
- </application></manifest>
三、自定义广播
下面我们学习自己制作一个广播。我们接着刚才的例子,继续写下去。
4、在MainBroadcastReceiver.java中填写如下代码:
java代码:
- package android.demo;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class MainBroadcastReceiver extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button b1 = (Button) findViewById(R.id.Button01);
- b1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //定义一个intent
- Intent intent = new Intent().setAction(
- "android.basic.lesson21.Hello").putExtra("yaoyao",
- "yaoyao is 189 days old ,27 weeks -- 2010-08-10");
- //广播出去
- sendBroadcast(intent);
- }
- });
- }
- }
5、更改 HelloBroadReceiver.java
内容如下:
java代码:
- package android.demo;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.util.Log;
- public class HelloBroadReciever extends BroadcastReceiver {
- //如果接收的事件发生
- @Override
- public void onReceive(Context context, Intent intent) {
- //对比Action决定输出什么信息
- if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
- Log.e("HelloBroadReciever", "BOOT_COMPLETED !!!!!!!!!!!!!!!!!!!!!!!!!");
- }
- if(intent.getAction().equals("android.basic.lesson21.Hello")){
- Log.e("HelloBroadReciever", "Say Hello to Yaoyao !!!!!!!!!!!!!!!!!!!!!!!!!");
- Log.e("HelloBroadReciever", intent.getStringExtra("yaoyao"));
- }
- //播放一首音乐
- MediaPlayer.create(context, R.raw.babayetu).start();
- }
- }
6、更改 AndroidManifest.xml
内容如下:
java代码:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android.basic.lesson21" android:versionname="1.0" android:versioncode="1">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:label="@string/app_name" android:name=".MainBroadcastReceiver">
- <intent -filter="">
- <action android:name="android.intent.action.MAIN">
- <category android:name="android.intent.category.LAUNCHER">
- </category></action></intent>
- </activity>
- <!-- 定义Broadcast Receiver 指定监听的Action 这里我们的接收器,接收了2个Action,一个系统的一个我们自定义的 -->
- <receiver android:name="HelloBroadReciever">
- <intent -filter="">
- <action android:name="android.intent.action.BOOT_COMPLETED">
- </action></intent>
- <intent -filter="">
- <action android:name="android.basic.lesson21.HelloYaoYao">
- </action></intent>
- </receiver>
- </application>
- <uses -sdk="" android:minsdkversion="8">
- </uses></manifest>
<ignore_js_op>