一 理论概述
1). 广播事件处理属于系统级的事件处理(一般事件处理是属于View 级的事件处理)
2). 一个应用可以在发生特定事件时发送 Broadcast ,系统中任何应用只要注册了对应Receiver就会接收到此 Broadcast
3). 一个应用如果对某个广播感兴趣,就可以注册对应的Receiver 来接收广播
4). 广播事件机制是应用程序(进程间)之间通信的一种手段
2. 相关API
1). Context
* sendBroadcast(Intent intent) : 发送一般广播
* sendOrderedBroadcast(Intent intent) : 发送有序广播
* registerReceiver(receiver, intentFilter) : 注册广播接收器
* unRegisterReceiver(receiver) : 解注册广播接收器
2). BroadcastReceiver
* onReceiver(Context context, Intent intent) : 接收到广播的回调
* abortBroadcast() : 中断广播的继续传播
* boolean isOrderedBroadcast() : 判断是否是有序广播
3. 常见广播
收到短信广播 :android.provider.Telephony.SMS_RECEIVED
开机完成广播 : Intent.ACTION_BOOT_COMPLETED
应用被卸载广播 : Intent.ACTION_PACKAGE_REMOVED
手机锁屏 : intent.ACTION_SCREEN_OFF
手机开屏 : Intent.ACTION_SCREEN_ON
二 发送广播
三 注册广播接收器
public class MyReceiver1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//处理广播
}
}
<receiver android:name="com.atguigu.l08_br.MyReceiver4">
<!-- priority指定当前receiver的优先级,最大为0-2147483647 -->
<intent-filter android:priority="2147483647">
<action android:name="com.atguigu.l08_br.MyReceiver.action2"/>
</intent-filter>
</receiver>
//创建receiver对象
MyReceiver2 receiver = new MyReceiver2();
//注册receiver
IntentFilter filter = new IntentFilter("com.atguigu.l08_br.MyReceiver1.action");
//注册receiver
registerReceiver(receiver , filter );
四 代码
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 发送一般广播
* @param v
*/
public void sendNormalBC(View v){
Intent intent = new Intent("com.atguigu.l08_br.MyReceiver1.action");
intent.putExtra("action", "hehe");
sendBroadcast(intent);
Toast.makeText(this, "发送一般广播", 0).show();
}
/**
* 发送有序广播
*/
public void sendOrderBC(View v){
Intent intent = new Intent("com.atguigu.l08_br.MyReceiver.action2");
intent.putExtra("action", "TATA");
sendOrderedBroadcast(intent , null);
Toast.makeText(this, "发送有序广播", 0).show();
}
}
动态注册接收广播
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 动态注册广播接收器
* @param v
*/
MyReceiver2 receiver;
public void registBR(View v){
if(receiver==null){
//创建receiver对象
receiver = new MyReceiver2();
//注册receiver
IntentFilter filter = new IntentFilter("com.atguigu.l08_br.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() {
super.onDestroy();
if(receiver!=null){
unregisterReceiver(receiver);
receiver=null;
}
}
}
静态注册接收广播
public class MyReceiver4 extends BroadcastReceiver {
public MyReceiver4(){
Log.e("TAG", "MyReceiver4()");
}
@Override
public void onReceive(Context arg0, Intent intent) {
String action = intent.getStringExtra("action");
Log.e("TAG", "MyReceiver4 onReceive()" + action);
//判断如果是有序广播
if(isOrderedBroadcast()){
//中断广播
abortBroadcast();
}
}
}
配置文件
<receiver android:name="com.atguigu.l08_br.MyReceiver4">
<!-- priority指定当前receiver的优先级,最大为0-2147483647 -->
<intent-filter android:priority="2147483647">
<action android:name="com.atguigu.l08_br.MyReceiver.action2"/>
</intent-filter>
</receiver>