|
@@ -0,0 +1,67 @@
|
|
|
+package com.fxy.base;
|
|
|
+
|
|
|
+import android.content.BroadcastReceiver;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.os.BatteryManager;
|
|
|
+import android.util.Log;
|
|
|
+
|
|
|
+import com.fxy.utils.JobUtils;
|
|
|
+
|
|
|
+public class DeviceStatusReceiver extends BroadcastReceiver {
|
|
|
+
|
|
|
+ private static final String TAG = "DeviceStatusReceiver";
|
|
|
+ @Override
|
|
|
+ public void onReceive(Context context, Intent intent) {
|
|
|
+ String action = intent.getAction();
|
|
|
+ if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
|
|
|
+ handleBatteryChanged(context, intent);
|
|
|
+ } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
|
|
|
+ handleScreenOn(context);
|
|
|
+ } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
|
|
|
+ handleScreenOff(context);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void handleBatteryChanged(Context context, Intent intent) {
|
|
|
+ int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
|
|
|
+ boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING;
|
|
|
+ int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
|
|
|
+ boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
|
|
|
+ boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
|
|
|
+
|
|
|
+ Log.d(TAG, "Charging state: " + isCharging);
|
|
|
+ Log.d(TAG, "USB charging: " + usbCharge);
|
|
|
+ Log.d(TAG, "AC charging: " + acCharge);
|
|
|
+
|
|
|
+ if (isCharging) {
|
|
|
+ onCharging(context);
|
|
|
+ } else {
|
|
|
+ onNotCharging(context);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void handleScreenOn(Context context) {
|
|
|
+ Log.d(TAG, "Screen turned ON");
|
|
|
+ // 屏幕打开时的操作
|
|
|
+ }
|
|
|
+
|
|
|
+ private void handleScreenOff(Context context) {
|
|
|
+ Log.d(TAG, "屏幕关闭时");
|
|
|
+ // 屏幕关闭时的操作
|
|
|
+ // 设备正在充电
|
|
|
+ JobUtils.deviceIdleJob(context);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void onCharging(Context context) {
|
|
|
+ // 当设备正在充电时的操作
|
|
|
+ Log.d(TAG, "Device is charging!");
|
|
|
+ // 设备正在充电
|
|
|
+ JobUtils.chargingJob(context);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void onNotCharging(Context context) {
|
|
|
+ // 当设备不在充电时的操作
|
|
|
+ Log.d(TAG, "Device is not charging!");
|
|
|
+ }
|
|
|
+}
|