如何计算Android设备电池容量?

huangapple go评论89阅读模式
英文:

How to Calculate Android Device Battery Capacity?

问题

目前,我正在进行一个Android项目,需要获取设备的电池容量。此函数从BatteryManager获取电池百分比和剩余电池电流,然后计算最大容量(mAh)。但我需要获取制造商声称的电池容量。

示例:

我的设备电池容量为4900毫安时(根据制造商),
但此函数返回4600毫安时。我需要获取4900毫安时。

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

        if (chargeCounter != null && capacity != null) {
            long value = (long) (((float) chargeCounter / (float) capacity) * 100f);
            return value;
        }
    }
    return -1;
}
英文:

Currently, I am working on an Android project for which I need to get the device's Battery Capacity. This function retrieves battery percentage and remaining battery current from BatteryManager and then calculates Maximum capacity (mAh). But I need to get the Battery capacity that the manufacturer claims.

Example:

> My device's battery capacity is 4900 mAh (As per the manufacturer),
> but this function returns 4600 mAh. I need to get that 4900 mAh.

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

        if (chargeCounter != null && capacity != null) {
            long value = (long) (((float) chargeCounter / (float) capacity) * 100f);
            return value;
        }
    }
    return -1;
}

答案1

得分: 2

这不会起作用。首先,你会遇到四舍五入误差。这些都不是精确的东西。其次,你假设制造商报告的电池容量和Android将其视为100%的电池容量是相同的。它们可能相同,但不一定-Android可能决定将电池的90%视为满电,以便剩下的10%用于紧急电源/关机/电池维护(有些电池实际运行到0会受到物理损害)。或者它可能保留电力以应对环境条件(例如,电池在不同的温度下工作方式不同)。制造商的4900是在最佳条件下的最大充电量,而不是实际有用的数字。它唯一有价值的地方是与同一技术的另一块电池进行直接比较。

英文:

That's not going to work. First off, you're going to have rounding errors. None of this stuff is exact. Secondly, you're assuming that the capacity of the battery as reported by the manufacturer and the capacity of the battery Android treats as 100% are the same. They may be, but they don't have to be- Android might decide to treat 90% of battery as full, so it has the remaining 10% for emergency power/shutdown/battery maintenance (some batteries are physically harmed if actually run to 0). Or it may reserve power to account for environmental conditions (batteries work differently depending on temperature, for example). 4900 from the manufacturer is the maximum amount of charge under optimal conditions, not an actual useful number. Its only value is in direct comparison to another battery of the same technology.

huangapple
  • 本文由 发表于 2023年7月7日 00:30:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76630868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定