在安卓系统中估计剩余电池时间

3
我正在尝试使用以下方法在Android中估算大约剩余的电池时间,但在所有时间上都不准确。请提供任何建议以提高准确性,
  • 当电池达到20%,15%时,以下方法将在首选项中保存当前时间戳
  • 它将从上述2个保存的时间计算近似估计值
谢谢
public static String getBatteryEstimation(Context context){
    String contentText = "";
    try{
        //timestamp recorded when battery reach 20%
        long time1 = PreferencesUtil.getInstance().getLong(PreferencesUtil.KEY_BATTERY_THERESHOLD_TIME_1);

        //timestamp recorded when battery reach 15%
        long time2 = PreferencesUtil.getInstance().getLong(PreferencesUtil.KEY_BATTERY_THERESHOLD_TIME_2);

        long timeDiffInMillis = time2 - time1;
        long timeTakenFor1Percentage = Math.round(timeDiffInMillis/5);
        long timeLastForNext15Percentage = timeTakenFor1Percentage * 15;

        long hoursLast = Math.abs(TimeUnit.MILLISECONDS.toHours(timeLastForNext15Percentage));
        long minutesLast = Math.abs(TimeUnit.MILLISECONDS.toMinutes(timeLastForNext15Percentage)-                             TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(timeLastForNext15Percentage)));

        String timeLastMessage = "";
        if(hoursLast > 0){
            timeLastMessage = String.valueOf(minutesLast)+" hour(s) "+String.valueOf(minutesLast) + " min(s)";
        } else {
            timeLastMessage = String.valueOf(minutesLast) + " min(s)";
        }

        DateFormat dateFormat = new SimpleDateFormat("HH:mm dd MMM");
        Date date = new Date();
        contentText = String.format(context.getString(R.string.battery_low_content_1), timeLastMessage, dateFormat.format(date));
    } catch (Exception e){
        e.printStackTrace();
    }

    return contentText;
}

你是否考虑过采集更多的样本,然后基于这些样本进行预测呢?例如,每隔%减少采集一个样本,然后使用已知算法之一进行预测? :) - JohanShogun
http://android.stackexchange.com/questions/45734/how-does-the-android-os-estimate-remaining-battery-time - rohitanand
你能发布一下Preference Util类吗? - Kaushal28
1个回答

2
估算电池剩余寿命基于分析。正如其他人所说,您需要倾听电池电量的变化,并且此外您还要跟踪它们。经过一段时间后,您将拥有足够的数据来计算电池平均持续时间。此外,您还知道什么时候电池会快速耗尽、什么时候会慢速耗尽,因此您可以根据此改善估计。另外,您也将知道用户何时充电设备。有很多事件可以通过电池电量进行跟踪。此外,您还可以跟踪屏幕开启或关闭的时间。计算剩余电池寿命的算法取决于你:)
你可以尝试这个方法:
收集从电池统计信息中获取的所有信息并计算出总使用量。然后计算每秒钟的使用量,即电池每秒钟被消耗了多少。
获取电池容量的mAh值,并使用以下公式计算剩余寿命:总容量÷消耗速度
希望这至少能解释一点。

如何获取当前电池容量(mAh)?(不是总电池容量) - Karthi Ponnusamy
1
以上示例链接是用于总电池容量,而不是当前电池容量。 - Karthi Ponnusamy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接