Значение ёмкости АКБ в Андроид

Интересует где находится значение предварительной ёмкости АКБ в Андроид и можно ли это значение изменить.

Например, приложение Aida64 показывает ёмкость АКБ 4к мАч, откуда она берет это значение?


Ответы (1 шт):

Автор решения: Sergei Buvaka

Вот метод который возвращает общую емкость батареи.

public long getBatteryCapacity(Context context) {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        BatteryManager mBatteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
        Integer chargeCounter = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);
        Integer capacity = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);

        if(chargeCounter == Integer.MIN_VALUE || capacity == Integer.MIN_VALUE)
            return 0;

        return (chargeCounter/capacity) *100;
    }
    return 0;
}

К сожалению, такой подход не всегда работает. В таких случаях вы можете использовать Java Reflection, чтобы получить значение, возвращаемое методом getBatteryCapacity() для com.android.internal.os.PowerProfile:

public double getBatteryCapacity(Context context) {
    Object mPowerProfile;
    double batteryCapacity = 0;
    final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";

    try {
        mPowerProfile = Class.forName(POWER_PROFILE_CLASS)
                .getConstructor(Context.class)
                .newInstance(context);

        batteryCapacity = (double) Class
                .forName(POWER_PROFILE_CLASS)
                .getMethod("getBatteryCapacity")
                .invoke(mPowerProfile);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return batteryCapacity;

}

Вот источник.

→ Ссылка