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