一.理论概述
- 广播事件处理属于系统级的事件处理(一般事件处理是属于View级的事件处理)
- 一个应用可以在发生特定事件时发送Broadcast, 系统中任何应用只要注册了对应Receiver就会接收到此Broadcast
- 一个应用如果对某个广播感兴趣, 就可以注册对应的Receiver来接收广播
- 广播事件机制是应用程序(进程间)之间通信的一种手段
二.相关API
Context
sendBroadcast(Intent intent) : 发送一般广播
sendOrderedBroadcast(Intent intent) : 发送有序广播
registerReceiver(receiver, intentFilter) : 注册广播接收器
unRegisterReceiver(receiver) : 解注册广播接收器
BroadcastReceiver
onReceive(Context context, Intent intent) : 接收到广播的回调
abortBroadcast() : 中断广播的继续传播
boolean isOrderedBroadcast() : 判断是否是有序广播
常见系统广播:
收到短信广播 : android.provider.Telephony.SMS_RECEIVED
开机完成广播 : Intent.ACTION_BOOT_COMPLETED
应用被卸载广播 : Intent.ACTION_PACKAGE_REMOVED
手机锁屏 : Intent.ACTION_SCREEN_OFF
手机开屏 : Intent.ACTION_SCREEN_ON
三.代码实现:
1.编写广播接收器类
package com.example.boardreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* 静态注册的receiver
* @author Xiaocici
*
*/
public class MyReceiver1 extends BroadcastReceiver {
public MyReceiver1() {
Log.e("TAG", "MyReceiver1()");
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getStringExtra("action");
Log.e("TAG","MyReceiver1 onReceive()" + action);
}
}
package com.example.boardreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* 动态注册的receiver
* @author Xiaocici
*
*/
public class MyReceiver2 extends BroadcastReceiver {
public MyReceiver2() {
Log.e("TAG", "MyReceiver2()");
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getStringExtra("action");
Log.e("TAG","MyReceiver2 onReceive()" + action);
}
}
2.注册广播接收器
方式一: 配置文件注册(静态注册)
<span style="white-space:pre"> </span><receiver android:name="com.atguigu.t6_br.MyBroadcastReceiver">
<!--priority指定当前receiver的优先级, 值是0-1000, 越大越好 -->
<intent-filter android:priority="500">
<action android:name="com.atguigu.t6_br.mybroadcastreceiver"/>
</intent-filter>
</receiver>
例:
<receiver android:name="com.example.boardreceiver.MyReceiver1">
<intent-filter >
<action android:name="com.example.boardreceiver.MyReceiver1.action"/>
</intent-filter>
</receiver>
方式二: 硬编码注册(动态注册)
MyBroadcastReceiver2 receiver2 = new MyBroadcastReceiver2();
IntentFilter filter = new IntentFilter(action);
registerReceiver(receiver2, filter);
例:
package com.example.boardreceiver;
import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private MyReceiver2 receiver;
/*
* 注册广播接收器
*/
public void registBR(View v) {
if (receiver == null) {
// 创建Receiver对象
receiver = new MyReceiver2();
// 创建过滤器对象
IntentFilter filter = new IntentFilter(
"com.example.boardreceiver.MyReceiver1.action");
// 注册Receiver
registerReceiver(receiver, filter);
Toast.makeText(this, "注册广播接收器", 0).show();
} else {
Toast.makeText(this, "已经注册了广播接收器", 0).show();
}
}
/*
* 解注册广播接收器
*/
public void unRegistBR(View v) {
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
Toast.makeText(this, "解注册广播接收器", 0).show();
} else {
Toast.makeText(this, "还未注册广播接收器", 0).show();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
}
}
3.发送广播
一般广播:
//发送一般广播
public void sendNormalBC(View v){
Intent intent = new Intent("com.example.boardreceiver.MyReceiver1.action");
intent.putExtra("action", "benben");
sendBroadcast(intent );
Toast.makeText(this, "发送一般广播", 0).show();
}
有序广播:
//发送有序广播
public void sendOrderBC(View v){
Intent intent = new Intent("com.example.boardreceiver.MyReceiver.action2");
intent.putExtra("action", "chacha");
sendOrderedBroadcast(intent, null);
Toast.makeText(this, "发送有序广播", 0).show();
}
有序广播的注册:
<receiver android:name="com.example.boardreceiver.MyReceiver3">
<intent-filter android:priority="1000">
<action android:name="com.example.boardreceiver.MyReceiver.action2"/>
</intent-filter>
</receiver>
<receiver android:name="com.example.boardreceiver.MyReceiver4">
<intent-filter android:priority="2147483647">//优先级
<action android:name="com.example.boardreceiver.MyReceiver.action2"/>
</intent-filter>
</receiver>
如果需中断广播:
package com.example.boardreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* 静态注册的receiver
*
* @author Xiaocici
*
*/
public class MyReceiver4 extends BroadcastReceiver {
public MyReceiver4() {
Log.e("TAG", "MyReceiver4()");
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getStringExtra("action");
Log.e("TAG", "MyReceiver4 onReceive()" + action);
if (isOrderedBroadcast()) {
abortBroadcast();//中断广播
}
}
}