前言
此篇博客用于记录系统的各种广播状态
广播监听开机
注册静态广播
<receiver android:name=".receiver.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
添加权限
<!-- 接收开机广播的权限,开机是很危险的行为,所以需要此权限 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
接收广播
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
L.e("设备开机了");
}
}
}
广播监听关机
注册静态广播
<receiver android:name=".receiver.ShutdownReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN"/>
</intent-filter>
</receiver>
接收广播
public class ShutdownReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.ACTION_SHUTDOWN")){
L.e("设备关机了...");
}
}
}
网络变化广播
注册
IntentFilter networkChangeFilter = new IntentFilter();
networkChangeFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
NetWorkChangeReceiver netWorkChangeReceiver = new NetWorkChangeReceiver();//网络变化广播
registerReceiver(netWorkChangeReceiver, networkChangeFilter);
接收
public class NetWorkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null) {
return;
}
String action = intent.getAction();
if (TextUtils.isEmpty(action)) {
return;
}
if (!TextUtils.equals(action, ConnectivityManager.CONNECTIVITY_ACTION)) {
return;
}
// 监听网络连接,包括wifi和移动数据的打开和关闭,以及连接上可用的连接都会接到监听
Log.i("hcz", " CONNECTIVITY_ACTION ");
//获取联网状态的NetworkInfo对象
NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (info == null) {
return;
}
if (NetworkInfo.State.CONNECTED != info.getState() || !info.isAvailable()) {
EventBus.getDefault().post(new NetChangerEvent(false));
return;
}
int type = info.getType();
//如果当前的网络连接成功并且网络连接可用
if (type == ConnectivityManager.TYPE_WIFI || type == ConnectivityManager.TYPE_MOBILE) {
Log.i("hcz", getConnectionType(info.getType()) + "连上");
EventBus.getDefault().post(new NetChangerEvent(true));
}
}
/**
* 获取连接类型
*
* @param type type
* @return String
*/
private String getConnectionType(int type) {
String connType = "";
if (type == ConnectivityManager.TYPE_MOBILE) {
connType = "3G网络数据";
} else if (type == ConnectivityManager.TYPE_WIFI) {
connType = "WIFI网络";
}
return connType;
}
}
时间变化广播
注册
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_TIME_TICK); //时间变化广播
registerReceiver(new TimeChangeReceiver(), intentFilter);
接收
public class TimeChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_TIME_TICK)) {
LogUtils.d("时间变化");
}
}
}
灭屏Or亮屏监听
注册
mPowerReceiver = PowerReceiver()
val intent = IntentFilter()
intent.addAction(Intent.ACTION_SCREEN_ON)
intent.addAction(Intent.ACTION_SCREEN_OFF)
registerReceiver(mPowerReceiver, intent)
监听
public class PowerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("ytzn", "onReceive: " + intent.getAction());
}
}
多媒体音量变化广播监听
/**
* 多媒体音量变化广播监听
*/
inner class VolumeReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action.equals("android.media.VOLUME_CHANGED_ACTION")) {
val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
val currVolume: Int = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
updateVolumeView(currVolume)
}
}
}
/**
* 初始化音量
*/
private fun initVolume() {
mVolumeReceiver = VolumeReceiver()
val filter = IntentFilter()
filter.addAction("android.media.VOLUME_CHANGED_ACTION")
context.registerReceiver(mVolumeReceiver, filter)
val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
val currVolume: Int = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) // 当前的媒体音量
updateVolumeView(currVolume)
}
应用安装/卸载/更新
inner class AppUpdateReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
var packageName = intent.getDataString()
Log.e(TAG, packageName + "安装成功")
} else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
var packageName = intent.getData().getSchemeSpecificPart() //获取包名
Log.e(TAG, packageName + "替换成功")
} else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
var packageName = intent . getData ().getSchemeSpecificPart()
Log.e(TAG, packageName + "卸载成功")
}
}
}
public static void registerReceiver(Context context) {
mIntentFilter = new IntentFilter();
mIntentFilter.addDataScheme("package");
mIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
mIntentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
mIntentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
context.registerReceiver(mReceiver, mIntentFilter);
}