【问题标题】:Get the exact battery level on android在 android 上获取确切的电池电量
【发布时间】:2015-01-20 10:33:36
【问题描述】:

所以我目前在我的android应用中实现了以下常用的获取当前电池电量的方法。

int getBatteryPercent(){
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = this.registerReceiver(null, ifilter);
    int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    return level;
}

问题是它返回一个整数 0-100。为了确保我的一种算法的准确性,我需要能够更精确地测量电池电量。我已经看到一些电池管理应用程序显示了可以完美运行的 mV。如果有人知道如何获得这个值,任何帮助将不胜感激。

澄清一下:我需要的是当前电池电量,而不是电池的总容量。

谢谢!

----------更新---------- -----

我现在使用以下代码:

    // Method for getting the current battery percentage
int getBatteryPercent(){
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = this.registerReceiver(null, ifilter);
    int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    return level;
}

// Method for getting the total battery capacity
double getBatteryCapacity() {
    double battCapacity = 0.0d;
    Object mPowerProfile_ = null;

    Log.d("Debug", "in getBatteryCapacity()");

    final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";

    try {
        mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS)
                .getConstructor(Context.class).newInstance(this);
    } catch (Exception e) {
        e.printStackTrace();
        Log.d("Debug", "Try 1 - Exception e: " + e.toString());
    }

    try {
        battCapacity = (Double) Class
                .forName(POWER_PROFILE_CLASS)
                .getMethod("getAveragePower", java.lang.String.class)
                .invoke(mPowerProfile_, "battery.capacity");
        Log.d("Debug", "battCapacity is now: " + battCapacity);
    } catch (Exception e) {
        e.printStackTrace();
        Log.d("Debug", "Try 2 - Exception e: " + e.toString());
    }

    Log.d("Debug", "Leaving getBatteryCapacity()");
    return battCapacity;
}

// Method for getting the exact current battery level
double getExactBattery(){
    Log.d("Debug", "In getExactBattery()");
    float batteryPercentage = getBatteryPercent();
    Log.d("Debug", "batteryPercentage = " + batteryPercentage);
    double totalCapacity = getBatteryCapacity();
    Log.d("Debug", "totalCapacity = " + totalCapacity);
    double currLevel = (totalCapacity * (double)batteryPercentage) / 100.0d;
    Log.d("Debug", "currLevel = " + currLevel);
    return currLevel;
}

但是 getBatteryCapacity 方法给了我一个 0.0 的回报:

01-20 06:37:27.314    7162-7162/com... D/Debug﹕ In getExactBattery()
01-20 06:37:27.324    7162-7162/com... D/Debug﹕ batteryPercentage = 47.0
01-20 06:37:27.329    7162-7162/com... D/Debug﹕ in getBatteryCapacity()
01-20 06:46:00.644    9207-9207/com... D/Debug﹕ battCapacity is now: 0.0
01-20 06:37:27.329    7162-7162/com... D/Debug﹕ Leaving getBatteryCapacity()
01-20 06:37:27.329    7162-7162/com... D/Debug﹕ totalCapacity = 0.0
01-20 06:37:27.329    7162-7162/com... D/Debug﹕ currLevel = 0.0

我有一个在 Android 4.4.4 上运行的 Droid Ultra,此代码不起作用,它返回 0.0,但是当在具有多个 android 版本的模拟器上运行时,它实际上可以工作。我很想知道在 Droid Ultra 上测试的其他用户是否有同样的问题。上面的代码应该可以在大多数设备上运行。

这实际上不是确切的电池电量。如果电池百分比为 97% 且总容量为 1000,则使用此算法,确切的电池电量将显示为 970。现在,如果实际电池百分比为 96.7%,我的 getBatteryPercent 将返回 97。这将导致 SAME EXACT ANSWER .所以这个算法不是这个问题的答案。我还在寻找这个问题的答案!!!我需要比 +-1% 更精确的电池电量测量...

----------更新---------- -----

我发现,如果您在监听 ACTION_BATTERY_CHANGED 的广播接收器中使用 getIntExtra("voltage", -1)。它为您提供 mV。我想知道这个值是否足够可靠以用于电池寿命预测。有谁知道我可以在任何时间点获取电压,而无需依赖广播接收器的最新更新?

编辑:事实证明它不可行。 this graph 表明电池电压是电池容量的一个非常差的指标。

【问题讨论】:

  • @user87049 我已经阅读了那个问题/答案。如果答案中的代码是检索总电池容量或当前电池电量,则在任何地方都没有明确说明。我需要当前等级
  • 这是什么应用程序?您可以使用 dex2jar 和 jd-gui 并进行关键字搜索,看看这是否有助于您了解它们是如何获得价值的。
  • @JaredRummler 一些电池校准应用程序。我不记得了,但我肯定记得看到过 mAH 值
  • @JaredRummler 是的。该应用程序在应用商店中被命名为“电池校准”。它给出了当前 mV 的读数

标签: java android battery batterylevel


【解决方案1】:

根据@user87049的链接获取电池容量,并使用自己的函数获取当前电池百分比并将这两者结合得到当前容量。

int batteryPercentage = getBatteryPercentage() //Your function
double batteryCapacity = getBatteryCapacity() //code from link from user87049. Change it to return value
double currentCapacity = (batteryPercentage * batteryCapacity) / 100

【讨论】:

  • 我不确定。如果你把它组合成一个函数,你为什么要一个更简单的方法?
  • 似乎不起作用。链接中的代码给了我 0.0 的回报。我已经用我的代码更新了我的问题
  • 你能调试getBatteryCapacity()函数吗?我认为您正在输入其中一个 catch 子句
  • 我明白,但是您的 battCapacity 初始化为 0.0,并且仅设置在 try 内。你确定你没有进入那个尝试的捕获子句吗?同时,我还调试了您的代码,这是我的输出:01-20 12:36:16.961 23239-23239/hooff.nl.dummydroid D/Debug﹕ In getExactBattery() 01-20 12:36:36.754 23239-23239/hooff.nl.dummydroid D/Debug﹕ batteryPercentage = 100.0 01-20 12:37:21.368 23239-23239/hooff.nl.dummydroid D/Debug﹕ totalCapacity = 3260.0 01-20 12:38:09.294 23239-23239/hooff.nl.dummydroid D/Debug﹕ currLevel = 3260.0 在 Nexus7 2012 上使用 Android 5.0.2 测试
  • 太奇怪了。我在 try 中添加了一个 Log.d,它打印并说在分配之后,battCapacity 仍然是 0.0。在 4.4.4 上运行 Droid Ultra。自己查看更新
【解决方案2】:

这个功能会帮助你。

public float getBatteryPCT()
{
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = this.registerReceiver(null, ifilter);
        int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        //Check if charging.
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
        //Check if charger plugged in.
        int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        //check if charging via USB.
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        //check if charging via AC.
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        float batteryPct = level / (float)scale;
        //Get the current battery percentage.
        return batteryPct*100;
}

【讨论】:

  • 根本没有帮助,返回一个 0-100 之间的值,他需要比这更细。
【解决方案3】:

根据this answer,可以通过以下方式获取电池mAH值:

public double getBatteryCapacity() {
    Object mPowerProfile_ = null;

    final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";

    try {
        mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS)
                .getConstructor(Context.class).newInstance(this);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        double batteryCapacity = (Double) Class
                .forName(POWER_PROFILE_CLASS)
                .getMethod("getAveragePower", java.lang.String.class)
                .invoke(mPowerProfile_, "battery.capacity");
        return batteryCapacity;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}

【讨论】:

  • 检查我的问题更新,这段代码给了我 0.0
  • 检查是否捕获到任何异常,查看日志了解是否有问题
猜你喜欢
  • 1970-01-01
  • 2014-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多