android - 估计android中的剩余电池时间

标签 android batterymanager

我正在尝试估计大约。使用以下方法在 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;
}

最佳答案

估计剩余电池生命周期基于分析。正如其他人所说,您必须监听电池电量变化,此外您还必须跟踪它们。一段时间后,您将有足够的数据来计算电池的平均使用时间。此外,您还知道什么时候电池耗电快,什么时候耗电慢,因此您可以据此改进您的估计。您还将知道用户在什么时间为设备充电。有很多事件可以跟踪并使用电池电量。此外,您还可以跟踪屏幕何时打开或关闭。计算剩余电池生命周期的算法取决于你:)

你可以试试这个:

从电池统计中收集所有信息,并统计总使用量。然后计算每秒的使用量,每秒耗尽多少电量。

获取以 mAh 为单位的电池容量,并使用以下公式计算剩余电量:每种使用速度的总容量

我希望这能解释(至少一点点)。

关于android - 估计android中的剩余电池时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42107895/

相关文章:

Java Android 无法正确写入和读取文件

android - 有没有办法在 webview 中实现快速滚动拇指?

android - 无法实例化接收器 com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver

java - 如何在手机屏幕锁定时暂停计时器

ios - 当前电池状态总是在 ios 中返回 "Charging"

android - 您可以手动触发 ACTION_BATTERY_LOW 吗?

android - 检查设备是否已插入

安卓工作室 : Resource file does not exist in library project