我尝试获取有关Android设备电池的一些信息,例如实际电流,电压,...我成功地得到了电压,电平,...但不幸的是,我有一部使用 API 16 运行的手机,并且此 API 不存在参数 BatteryManager.BATTERY_PROPERTY_CURRENT_NOW,因为它出现在 API 21 中。
那么,如何获取当前信息和电池容量信息,例如此设备?
提前谢谢。
因为电池管理器与API 16一起工作。以下实用程序很有用。
package com.example.utils;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import com.example.app.AppContext;
import com.example.app.AppManager;
public class BatteryUtils {
public static Intent getBatteryIntent() {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
return AppContext.getInstance().registerReceiver(null, ifilter);
}
public static int getScale(Intent intent) {
return intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
}
public static int getLevel(Intent intent) {
return intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
}
public static int getChargeStatus(Intent intent) {
return intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
}
public static boolean isCharging(int status) {
return status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
}
public static boolean isCharging(Intent intent) {
int status = getChargeStatus(intent);
return isCharging(status);
}
public static void test() {
Intent intent = BatteryUtils.getBatteryIntent();
DebugUtils.log("isCharging:" + BatteryUtils.isCharging(intent) + ";level:" + BatteryUtils.getLevel(intent) + ";Scale:" + BatteryUtils.getScale(intent));
}
}
您可以尝试使用BatteryManager。它适用于 API 16。
https://developer.android.com/reference/android/os/BatteryManager.html
有了它,您可以获取状态(正在充电,充满,正在放电),容量,是否使用USB或无线充电,电池健康状况等