You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android平台Java/Kotlin获取小数形式当前电池电量百分比方法

在Android平台获取小数形式的电池电量百分比

可以实现。Android平台支持通过系统API或读取系统文件的方式获取精确到小数的电池电量百分比,以下是Kotlin和Java的具体实现方案:


Kotlin实现

核心逻辑

通过PowerProfile获取电池总容量(毫安时),结合BatteryManager提供的剩余容量计算比值,得到带小数的百分比。若设备不支持精确容量数据,会自动降级到整数精度计算。

import android.content.Context
import android.os.BatteryManager
import com.android.internal.os.PowerProfile

fun getBatteryPercentageDecimal(context: Context): Double? {
    val batteryManager = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager

    // 读取剩余容量(微安时转换为毫安时)
    val remainingMicroAh = batteryManager.getIntExtra(BatteryManager.EXTRA_CHARGE_COUNTER, -1)
    val remainingCapacity = remainingMicroAh / 1000.0
    if (remainingCapacity <= 0) return null

    // 读取电池总容量
    val totalCapacity = try {
        PowerProfile(context).getBatteryCapacity()
    } catch (e: Exception) {
        // 若PowerProfile获取失败,使用LEVEL/SCALE计算整数百分比作为降级方案
        val scale = batteryManager.getIntExtra(BatteryManager.EXTRA_SCALE, 100).toDouble()
        val level = batteryManager.getIntExtra(BatteryManager.EXTRA_LEVEL, 0).toDouble()
        return (level / scale) * 100
    }

    if (totalCapacity <= 0) return null

    // 计算并保留1位小数(可根据需求调整保留位数)
    return String.format("%.1f", (remainingCapacity / totalCapacity) * 100).toDouble()
}

注意事项

  • PowerProfile属于系统内部API,部分定制ROM可能存在兼容性问题,需捕获异常做降级处理。
  • Android 10及以上版本,需在Manifest中声明android.permission.BATTERY_STATS权限,并请求运行时权限。
  • EXTRA_CHARGE_COUNTER并非所有设备都支持,若返回-1则说明设备不提供该数据,此时只能使用整数精度的计算结果。

Java实现

import android.content.Context;
import android.os.BatteryManager;
import com.android.internal.os.PowerProfile;

public class BatteryUtils {
    public static Double getBatteryPercentageDecimal(Context context) {
        BatteryManager batteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
        if (batteryManager == null) return null;

        // 读取剩余容量(微安时转换为毫安时)
        int remainingMicroAh = batteryManager.getIntExtra(BatteryManager.EXTRA_CHARGE_COUNTER, -1);
        double remainingCapacity = remainingMicroAh / 1000.0;
        if (remainingCapacity <= 0) return null;

        // 读取电池总容量
        double totalCapacity;
        try {
            PowerProfile powerProfile = new PowerProfile(context);
            totalCapacity = powerProfile.getBatteryCapacity();
        } catch (Exception e) {
            // 降级到整数精度计算
            int scale = batteryManager.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
            int level = batteryManager.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            return (level * 100.0) / scale;
        }

        if (totalCapacity <= 0) return null;

        // 计算并保留1位小数
        double percentage = (remainingCapacity / totalCapacity) * 100;
        return Double.parseDouble(String.format("%.1f", percentage));
    }
}

补充:系统文件读取方式(需特殊权限)

部分root设备或拥有特定系统权限的应用,可通过读取电池相关系统文件计算精确电量:

// Kotlin示例,需READ_EXTERNAL_STORAGE权限或root权限
fun getBatteryDecimalFromSystemFiles(): Double? {
    return try {
        val remainingCharge = java.io.File("/sys/class/power_supply/battery/charge_counter")
            .readText().trim().toDouble() / 1000
        val fullCharge = java.io.File("/sys/class/power_supply/battery/full_charge_counter")
            .readText().trim().toDouble() / 1000
        if (fullCharge > 0) (remainingCharge / fullCharge) * 100 else null
    } catch (e: Exception) {
        null
    }
}

内容的提问来源于stack exchange,提问作者Krithick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 07:13:22