Android开发 广播监听系统的各种状态

时间:2022-03-02 05:34:49

前言

  此篇博客用于记录系统的各种广播状态

广播监听开机

  注册静态广播

        <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