天天看点

Android 获取电池温度

1. Demo 下载

https://github.com/sufadi/BatteryInfo

2. 电池温度

// 当前电池温度
import static android.os.BatteryManager.EXTRA_TEMPERATURE;

    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 temperature = intent.getIntExtra(EXTRA_TEMPERATURE, -1);

           

3. 电池健康状态-温度过低

private String getHealth(int health) {
        String result = getString(R.string.battery_health_unknow);

        switch (health) {
            case BATTERY_HEALTH_UNKNOWN:// 未知
                break;
            case BATTERY_HEALTH_GOOD:// 良好
                result = getString(R.string.battery_health_good);
                break;
            case BATTERY_HEALTH_OVERHEAT:// 过热
                result = getString(R.string.battery_health_overheat);
                break;
            case BATTERY_HEALTH_DEAD: // 没电
                result = getString(R.string.battery_health_dead);
                break;
            case BATTERY_HEALTH_UNSPECIFIED_FAILURE: // 未知错误
                result = getString(R.string.battery_health_unspecified_failure);
                break;
            case BATTERY_HEALTH_OVER_VOLTAGE:// 过电压
                result = getString(R.string.battery_health_over_voltage);
                break;
            case BATTERY_HEALTH_COLD: // 温度过低
                result = getString(R.string.battery_health_cold);
                break;
        }

        return result;
    }
           

底层上报数值如下

enum {
    BATTERY_HEALTH_UNKNOWN = 1,
    BATTERY_HEALTH_GOOD = 2,
    BATTERY_HEALTH_OVERHEAT = 3,
    BATTERY_HEALTH_DEAD = 4,
    BATTERY_HEALTH_OVER_VOLTAGE = 5,
    BATTERY_HEALTH_UNSPECIFIED_FAILURE = 6,
    BATTERY_HEALTH_COLD = 7,
};

           

4.adb shell 查看电池温度

adb shell cat /sys/devices/platform/battery/Battery_Temperature

5. adb shell 设置电池温度

adb shell “echo 55 > /sys/devices/platform/battery/Battery_Temperature”

这里可以设置一些极限温度,手机一般会报高温和低温警告

继续阅读