Android开发 广播监听系统的各种状态
前言
此篇博客用于记录系统的各种广播状态
广播监听开机
注册静态广播
<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("设备关机了..."); } } }
end
相关推荐
Nostalgiachild 2020-11-13
韩伟佳 2020-10-09
wuleihenbang 2020-09-16
zzqLivecn 2020-07-09
chenjinlong 2020-06-10
yinbaoshiguang 2020-06-09
sgafdsg 2020-06-04
ustcrding 2020-06-03
chenjinlong 2020-06-03
AndroidGA 2020-06-01
安辉 2020-05-27
绿豆饼 2020-05-26
CNETNews 2020-05-26
xilove0 2020-05-12
绿豆饼 2020-05-12
ChainDestiny 2020-05-07
doomvsjing 2020-05-07
hqulyc 2020-05-05
lyccsu 2020-04-30