在android上获得准确的电池电量

所以我目前有以下常用的方法来获取我的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可以完美运行。 如果有人知道如何获得这个价值,任何帮助将非常感激。

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

谢谢!

———————— UPDATE ————————- –

我现在使用以下代码:

  // 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.现在,如果ACTUAL电池百分比为96.7%,我的getBatteryPercent将返回97.这将导致相同的答案。 所以这个算法不是这个问题的答案。 我仍然在寻找这个问题的答案! 我需要一个比+ -1%精确的电池电量测量…

———————— UPDATE ————————- –

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

编辑:结果certificate它不可行。 该图显示电池电压是电池容量的非常差的指标。

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

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

这个function对你有帮助。

 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; } 

根据这个答案 ,您可以通过以下方式获得Batter 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; }