Broadcast
Broadcast 是 Android 四大组件之一,是一种广泛运用在应用程序之间异步传输信息的机制。Broadcast 本质上是一个Intent 对象,差别在于 Broadcast 可以被多个 BroadcastReceiver 处理。BroadcastReceiver 是一个全局监听器,通过它的 onReceive() 可以过滤用户想要的广播进行操作。通过本章学习可以基本掌握Broadcast 相关的知识点。
1.广播的生命周期
Broadcast 继承关系如下:
java.lang.Object
↳ android.content.Context.BroadcastReceiver
1. 广播的生命周期
BroadcastReceiver 的主要声明周期方法onReceiver(),此方法执行完之后,BroadcastReceiver 实例将被销毁。广播默认是在主线程中执行,如果onReceiver() 方法处理事件超过10s,则应用将会发生ANR(Application Not Responding),此时建立工作线程的方法并不能解决此问题,因此建议:如处理耗时操作,请用 Service 代替。
2.四大组件之一,必须在Androidmainfest.xml中注册
<receiver
android:name="ReceiverMethod"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="String....." />
</intent-filter>
</receiver>
如不注册,将导致无法接收处理广播消息
3.广播的注册(静态广播、动态广播)
静态广播
// 1.开机广播监听方法,此广播特殊,需要添加权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver
android:name=".component.BroadcastReceiver.BootReceiverMethod"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
// 2. apk 状态改变的广播
<receiver
android:name=".component.BroadcastReceiver.AppModifyMethods"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_DATA_CLEARED" />
<action android:name="android.intent.action.PACKAGE_FIRST_LAUNCH" />
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
<action android:name="android.intent.action.MANAGE_PACKAGE_STORAGE" />
<data android:scheme="package" />
</intent-filter>
</receiver>
接收静态广播消息的处理方法
接收开机广播的处理方法
package com.android.program.programandroid.component.BroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.android.program.programandroid.component.BroadcastReceiver.Service.StartServiceMethods;
public class BootReceiverMethod extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 接收开机广播处理事情,比如启动服务
Intent mStartIntent = new Intent(context, StartServiceMethods.class);
context.startService(mStartIntent);
}
}
接收 apk 信息改变的广播处理方法
package com.android.program.programandroid.component.BroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AppModifyMethods extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String packageName = intent.getData().getSchemeSpecificPart();
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
Toast.makeText(context, packageName + " app 安装成功", Toast.LENGTH_SHORT).show();
}
if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
Toast.makeText(context, packageName + "app 被替换", Toast.LENGTH_SHORT).show();
}
if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
Toast.makeText(context, packageName + "app 卸载成功", Toast.LENGTH_SHORT).show();
}
if (intent.getAction().equals(Intent.ACTION_PACKAGE_CHANGED)) {
Toast.makeText(context, packageName + "app 状态发生改变", Toast.LENGTH_SHORT).show();
}
if (intent.getAction().equals(Intent.ACTION_INSTALL_PACKAGE)) {
Toast.makeText(context, packageName + "app 安装成功广播", Toast.LENGTH_SHORT).show();
}
if (intent.getAction().equals(Intent.ACTION_DATE_CHANGED)) {
Toast.makeText(context, packageName + "app 数据改变广播", Toast.LENGTH_SHORT).show();
}
if (intent.getAction().equals(Intent.ACTION_PACKAGE_FIRST_LAUNCH)) {
Toast.makeText(context, packageName + "app 第一次打开广播", Toast.LENGTH_SHORT).show();
}
if (intent.getAction().equals(Intent.ACTION_PACKAGE_RESTARTED)) {
Toast.makeText(context, packageName + "接收 app 被重置的广播", Toast.LENGTH_SHORT).show();
}
if (intent.getAction().equals(Intent.ACTION_MANAGE_PACKAGE_STORAGE)) {
Toast.makeText(context, packageName + "app storage 发生变化", Toast.LENGTH_SHORT).show();
}
}
}
动态广播
package com.android.program.programandroid.component.BroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;
/**
* Created by wangjie on 17/9/9.
*/
public class ScreenOnOffReceiver {
public static void ReceiverScreenOnOff(Context context) {
IntentFilter screenOffFilter = new IntentFilter();
screenOffFilter.addAction(Intent.ACTION_SCREEN_OFF);
screenOffFilter.addAction(Intent.ACTION_SCREEN_ON);
BroadcastReceiver mScreenOnOffReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Toast.makeText(context, "接收屏幕熄灭广播", Toast.LENGTH_SHORT).show();
}
if (action.equals(Intent.ACTION_SCREEN_ON)) {
Toast.makeText(context, "接收屏幕点亮广播", Toast.LENGTH_SHORT).show();
}
}
};
/**
* context.getApplicationContext()
* 在广播中注册广播时候需要注意,防止context 为空 ,引起空指针异常
* **/
context.registerReceiver(mScreenOnOffReceiver, screenOffFilter);
}
}
// 注意 : 动态广播注册方法registerReceiver如下
调用方法如下:
ScreenOnOffReceiver.ReceiverScreenOnOff(MainActivity.this);
4.广播的发送(正常、有序、持续)
1.发送正常广播的方法
5.广播接收(系统广播、自定义广播)
注意 :接收广播的方法
例如:接收开机广播的方法
实现BootReceiverMethod 继承 BroadcastReceiver
ublic class BootReceiverMethod extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 接收开机广播处理事情,比如启动服务
Intent mStartIntent = new Intent(context, StartServiceMethods.class);
context.startService(mStartIntent);
}
}
2.在Androidmainfest.xml 声明组件信息,并过滤开机完成Action
<receiver
android:name=".component.BroadcastReceiver.BootReceiverMethod"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
3.声明接收开机广播完成的权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />