您的位置:首页 > 其它

第二十一讲: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?

01
package
android.basic.lesson21;
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(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
}
3、在AndroidManifest.xml中注册此Receiver:

viewsource

print?

01
<?
xml

version
=
"1.0"

encoding
=
"utf-8"
?>
02
<
MANIFEST

package
=
"android.basic.lesson21"

android:versioncode
=
"1"

android:versionname
=
"1.0"

xmlns:android
=
"http://schemas.android.com/apk/res/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
>
4、发布程序,启动模拟器,可以在Logcat中看到:





同时能听到音乐播放的声音。说明我们确实接收到了系统启动的广播事件,并做出了响应。





三、自定义广播

下面我们学习自己制作一个广播。我们接着刚才的例子,继续写下去。

5、在MainBroadcastReceiver.java中填写如下代码:

viewsource

print?

01
package
android.basic.lesson21;
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
/**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
}
6、更改HelloBroadReceiver.java内容如下:

viewsource

print?

01
package
android.basic.lesson21;
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(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
}
7、更改AndroidManifest.xml内容如下:

viewsource

print?

01
<?
xml

version
=
"1.0"

encoding
=
"utf-8"
?>
02
<
MANIFEST

package
=
"android.basic.lesson21"

android:versioncode
=
"1"

android:versionname
=
"1.0"

xmlns:android
=
"http://schemas.android.com/apk/res/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
>
8、运行程序,点击按钮,查看LogCat,听听声音









好了,本讲就到这里。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: