1

我尝试获取有关 android 设备电池的一些信息,例如实际电流、电压……我成功获得了电压、电平……但不幸的是,我有一部使用 API 16 运行且参数 BatteryManager.BATTERY_PROPERTY_CURRENT_NOW 的手机没有此 API 不存在,因为它与 API 21 一起出现。

那么,如何获取此设备的当前信息和电池容量信息?

提前致谢。

4

2 回答 2

2

由于 BatteryManager 与 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));
    }

}
于 2016-07-12T15:46:38.327 回答
1

您可以尝试使用 BatteryManager。它适用于 API 16。

https://developer.android.com/reference/android/os/BatteryManager.html

有了它,您可以获得状态(充电、充满、放电)、容量、是否使用 USB 或无线充电、电池健康状况等

于 2016-07-12T15:42:57.460 回答