BroadcastReceiver 用于异步接收广播Intent。主要有两大类,用于接收广播的:
- 正常广播 Normal broadcasts(用
Context.sendBroadcast
()发送)是完全异步的。它们都运行在一个未定义的顺序,通常是在同一时间。这样会更有效,但意味着receiver不能包含所要使用的结果或中止的API。 - 有序广播 Ordered broadcasts(用
Context.sendOrderedBroadcast()
发送)每次被发送到一个receiver。所谓有序,就是每个receiver执行后可以传播到下一个receiver,也可以完全中止传播——不传播给其他receiver。 而receiver运行的顺序可以通过matched intent-filter 里面的android:priority来控制,当priority优先级相同的时候,Receiver以任意的顺序运行。
要注意的是,即使是Normal broadcasts,系统在某些情况下可能会恢复到一次传播给一个receiver。 特别是receiver可能需要创建一个进程,为了避免系统超载,只能一次运行一个receiver。
Broadcast Receiver 并没有提供可视化的界面来显示广播信息。可以使用Notification和Notification Manager来实现可视化的信息的界面,显示广播信息的内容,图标及震动信息。
生命周期
一个BroadcastReceiver 对象只有在被调用
onReceive(Context, Intent)的才有效的,当从该函数返回后,该对象就无效的了,结束生命周期。
因此从这个特征可以看出,在所调用的onReceive(Context, Intent)函数里,不能有过于耗时的操作,不能使用线程来执行。对于耗时的操作,请start service来完成。因为当得到其他异步操作所返回的结果时,BroadcastReceiver 可能已经无效了。
发送广播
事件的广播比较简单,构建Intent对象,可调用sendBroadcast(Intent)方法将广播发出。另外还有sendOrderedBroadcast(),sendStickyBroadcast()等方法,请查阅API Doc。
1.new Intent with action name
Intent intent = new Intent(String action);
或者 只是new Intent, 然后
intent.setAction(String action);
2.set data等准备好了后,in activity,
sendBroadcast(Intent); // 发送广播
接收广播
通过定义一个继承BroadcastReceiver类来实现,继承该类后覆盖其onReceiver方法,并在该方法中响应事件。
public
class SMSReceiver
extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// get data from SMS intent
Bundle bundle = intent.getExtras();
if (bundle != null){
// get message by "pdus"
Object[] objArray = (Object[]) bundle.get( "pdus");
// rebuild SMS
SmsMessage[] messages = new SmsMessage[objArray.length];
for ( int i=0; i < objArray.length; i++){
messages[i] = SmsMessage.createFromPdu(( byte[])objArray[i]);
StringBuilder str = new StringBuilder( "from: ");
str.append(messages[i].getDisplayOriginatingAddress());
str.append( "\nmessage:\n");
str.append(messages[i].getDisplayMessageBody());
Toast.makeText(context, str.toString(), Toast.LENGTH_LONG)
.show();
}
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
// get data from SMS intent
Bundle bundle = intent.getExtras();
if (bundle != null){
// get message by "pdus"
Object[] objArray = (Object[]) bundle.get( "pdus");
// rebuild SMS
SmsMessage[] messages = new SmsMessage[objArray.length];
for ( int i=0; i < objArray.length; i++){
messages[i] = SmsMessage.createFromPdu(( byte[])objArray[i]);
StringBuilder str = new StringBuilder( "from: ");
str.append(messages[i].getDisplayOriginatingAddress());
str.append( "\nmessage:\n");
str.append(messages[i].getDisplayMessageBody());
Toast.makeText(context, str.toString(), Toast.LENGTH_LONG)
.show();
}
}
}
}
注册Receiver
注册有两种方式:
1. 静态方式,在AndroidManifest.xml的application里面定义receiver并设置要接收的action。
1. 静态方式,在AndroidManifest.xml的application里面定义receiver并设置要接收的action。
<
receiver
android:name
=".SMSReceiver"
>
< intent-filter >
< action android:name ="android.provider.Telephony.SMS_RECEIVED" />
</ intent-filter >
</ receiver >
< intent-filter >
< action android:name ="android.provider.Telephony.SMS_RECEIVED" />
</ intent-filter >
</ receiver >
2. 动态方式, 在activity里面调用函数来注册,和静态的内容差不多。一个形参是receiver,另一个是IntentFilter,其中里面是要接收的action。
public
class HelloDemo
extends Activity {
private BroadcastReceiver receiver;
@Override
protected void onStart() {
super.onStart();
receiver = new CallReceiver();
registerReceiver(receiver, new IntentFilter( "android.intent.action.PHONE_STATE"));
}
@Override
protected void onStop() {
unregisterReceiver(receiver);
super.onStop();
}
}
private BroadcastReceiver receiver;
@Override
protected void onStart() {
super.onStart();
receiver = new CallReceiver();
registerReceiver(receiver, new IntentFilter( "android.intent.action.PHONE_STATE"));
}
@Override
protected void onStop() {
unregisterReceiver(receiver);
super.onStop();
}
}
一个receiver可以接收多个action的,即可以有多个intent-filter,需要在onReceive里面对intent.getAction(action name)进行判断。
个人推荐使用静态注册方式,由系统来管理receiver,而且程序里的所有receiver,可以在xml里面一目了然。而动态注册方式,隐藏在代码中,比较难发现。
而且动态注册,需要特别注意的是,在退出程序前要记得调用 Context.unregisterReceiver()方法。一般在activity的onStart()里面进行注册, onStop()里面进行注销。官方提醒,如果在Activity.onResume()里面注册了,就必须在Activity.onPause()注销。
而且动态注册,需要特别注意的是,在退出程序前要记得调用 Context.unregisterReceiver()方法。一般在activity的onStart()里面进行注册, onStop()里面进行注销。官方提醒,如果在Activity.onResume()里面注册了,就必须在Activity.onPause()注销。
Permission权限
要接收某些action,需要在AndroidManifest.xml里面添加相应的permission。例如接收SMS:
要接收某些action,需要在AndroidManifest.xml里面添加相应的permission。例如接收SMS:
<
uses-permission
android:name
="android.permission.RECEIVE_SMS"
/>
下面给出动态注册的接收来电的广播处理的CallReceiver的代码:
一种方式是直接读取intent.getStringExtra("incoming_number")来获取来电号码:
public
class CallReceiver
extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager teleManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
switch(teleManager.getCallState()){
case TelephonyManager.CALL_STATE_RINGING: //响铃
Toast.makeText(context, "Ringing: " + intent.getStringExtra( "incoming_number"), Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //接听
Toast.makeText(context, "OffHook: " + intent.getStringExtra( "incoming_number"), Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_IDLE: //挂断
Toast.makeText(m_context, "Idle: " + incomingNumber, Toast.LENGTH_LONG).show();
break;
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager teleManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
switch(teleManager.getCallState()){
case TelephonyManager.CALL_STATE_RINGING: //响铃
Toast.makeText(context, "Ringing: " + intent.getStringExtra( "incoming_number"), Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //接听
Toast.makeText(context, "OffHook: " + intent.getStringExtra( "incoming_number"), Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_IDLE: //挂断
Toast.makeText(m_context, "Idle: " + incomingNumber, Toast.LENGTH_LONG).show();
break;
}
}
}
另一种方式是通过PhoneStateListener的onCallStateChanged来监听状态的变化:
public
class CallReceiver
extends BroadcastReceiver {
private Context m_context;
@Override
public void onReceive(Context context, Intent intent) {
m_context = context;
TelephonyManager teleManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
teleManager.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged( int state, String incomingNumber) {
switch(state){
case TelephonyManager.CALL_STATE_RINGING: //响铃
Toast.makeText(m_context, "Ringing: " + incomingNumber, Toast.LENGTH_LONG)
.show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //接听
Toast.makeText(m_context, "OffHook: " + incomingNumber, Toast.LENGTH_LONG)
.show();
break;
case TelephonyManager.CALL_STATE_IDLE: //挂断
Toast.makeText(m_context, "Idle: " + incomingNumber, Toast.LENGTH_LONG)
.show();
break;
}
}} , PhoneStateListener.LISTEN_CALL_STATE);
}
}
private Context m_context;
@Override
public void onReceive(Context context, Intent intent) {
m_context = context;
TelephonyManager teleManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
teleManager.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged( int state, String incomingNumber) {
switch(state){
case TelephonyManager.CALL_STATE_RINGING: //响铃
Toast.makeText(m_context, "Ringing: " + incomingNumber, Toast.LENGTH_LONG)
.show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //接听
Toast.makeText(m_context, "OffHook: " + incomingNumber, Toast.LENGTH_LONG)
.show();
break;
case TelephonyManager.CALL_STATE_IDLE: //挂断
Toast.makeText(m_context, "Idle: " + incomingNumber, Toast.LENGTH_LONG)
.show();
break;
}
}} , PhoneStateListener.LISTEN_CALL_STATE);
}
}
因为这里监听的是通话的状态变化,所以这个receiver会被调用3次。
监听通话状态需要加上权限:
<
uses-permission
android:name
="android.permission.READ_PHONE_STATE"
/>
===========
小结:
1. 对于sendBroadCast的intent对象,需要设置其action name;
2. 推荐使用显式指明receiver,在配置文件AndroidManifest.xml指明;
3. 一个receiver可以接收多个action;
2. 推荐使用显式指明receiver,在配置文件AndroidManifest.xml指明;
3. 一个receiver可以接收多个action;
4. 每次接收广播都会重新生成一个接收广播的对象,再次调用onReceive;
5. 在BroadCast 中尽量不要处理太多逻辑问题,建议复杂的逻辑交给Activity 或者 Service 去处理。
二、Broadcast Receiver接收系统自带的广播
我们做一个例子,功能是在系统启动时播放一首音乐。
1、建立一个项目Lesson21_BroadcastReceiver,拷贝一首音乐进res/raw目录
2、建立HelloBroadcastReceiver.java 内容如下:
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(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 | } |
3、在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、发布程序,启动模拟器,可以在Logcat中看到:
同时能听到音乐播放的声音。说明我们确实接收到了系统启动的广播事件,并做出了响应。
三、自定义广播
下面我们学习自己制作一个广播。我们接着刚才的例子,继续写下去。
5、在MainBroadcastReceiver.java中填写如下代码:
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 | /** 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 | } |
6、更改 HelloBroadReceiver.java 内容如下:
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(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 | } |
7、更改 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 > |
8、运行程序,点击按钮,查看LogCat,听听声音
转载http://android.yaohuiji.com/archives/727