Android 获取电池是否充电状态、充电器类型

2019-07-14 00:45发布

0. demo 下载

https://github.com/sufadi/BatteryInfo

1. 电池广播获取充电状态

// 电池状态 import static android.os.BatteryManager.EXTRA_STATUS; // 未知 import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN; // 充电中 import static android.os.BatteryManager.BATTERY_STATUS_CHARGING; // 放电中 import static android.os.BatteryManager.BATTERY_STATUS_DISCHARGING; // 未充电 import static android.os.BatteryManager.BATTERY_STATUS_NOT_CHARGING; // 电池满 import static android.os.BatteryManager.BATTERY_STATUS_FULL; private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (null == intent) { return; } String action = intent.getAction(); if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { int status = intent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN); ... private String getStatus(int status) { String result = getString(R.string.battery_status_unknown); switch (status) { case BATTERY_STATUS_FULL: result = getString(R.string.battery_status_full);// 充满电 break; case BATTERY_STATUS_NOT_CHARGING: result = getString(R.string.battery_status_not_charging); // 未充电 break; case BATTERY_STATUS_DISCHARGING: result = getString(R.string.battery_status_discharging);// 放电中 break; case BATTERY_STATUS_CHARGING: result = getString(R.string.battery_status_charging);// 充电中 break; case BATTERY_STATUS_UNKNOWN:// 未知 break; } return result; }

2. 充电器类型

// 充电器类型 import static android.os.BatteryManager.EXTRA_PLUGGED; // 充电器 import static android.os.BatteryManager.BATTERY_PLUGGED_AC; // 其他 import static android.os.BatteryManager.BATTERY_PLUGGED_ANY; // USB import static android.os.BatteryManager.BATTERY_PLUGGED_USB; // 无线充电 import static android.os.BatteryManager.BATTERY_PLUGGED_WIRELESS; private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (null == intent) { return; } String action = intent.getAction(); if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { int plugged = intent.getIntExtra(EXTRA_PLUGGED, BATTERY_PLUGGED_ANY); ... private String getPlugged(int plugged) { String result = getString(R.string.battery_plugged_any); switch (plugged) { case BATTERY_PLUGGED_ANY: // 其他 break; case BATTERY_PLUGGED_AC: // 充电器 result = getString(R.string.battery_plugged_ac); break; case BATTERY_PLUGGED_USB: // USB result = getString(R.string.battery_plugged_usb); break; case BATTERY_PLUGGED_WIRELESS: // 无线充电 result = getString(R.string.battery_plugged_wireless); break; } return result; } 具体键值 // values of the "plugged" field in the ACTION_BATTERY_CHANGED intent. // These must be powers of 2. /** Power source is an AC charger. */ public static final int BATTERY_PLUGGED_AC = 1; /** Power source is a USB port. */ public static final int BATTERY_PLUGGED_USB = 2; /** Power source is wireless. */ public static final int BATTERY_PLUGGED_WIRELESS = 4; /** @hide */ public static final int BATTERY_PLUGGED_ANY = BATTERY_PLUGGED_AC | BATTERY_PLUGGED_USB | BATTERY_PLUGGED_WIRELESS;

3. 获取电池是否充电接口

package com.android.settings.fuelgauge; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.util.Log; public class InonePowerSaveUtil { public static final boolean IS_CHARGE_DISABLE = true; public static boolean isChargingDisable(Context context) { return IS_CHARGE_DISABLE && isCharging(context); } public static boolean isCharging(Context context) { Intent batteryBroadcast = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); // 0 means we are discharging, anything else means charging boolean isCharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) != 0; Log.d(InonePowerSaveUtil.class.getSimpleName(),"isCharging = " + isCharging ); return isCharging; } }