剩余时间,直到充电完成,iOS [英] time remaining until charge is complete,iOS
问题描述
我使用的是:
UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
float batLeft = [myDevice batteryLevel];
int i = [myDevice batteryState];
int batinfo = batLeft * 100;
以查找电池状态。
我正在寻找,如何找到剩余的时间,直到充电完成。
例如:1小时20分钟剩余。
我如何以编程方式找到它?
to find the battery status. I am looking out to find, how to find the time remaining until the charge is complete. ex: 1 hour 20 min remaining. How can I find it programmatically?
推荐答案
=http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html>官方文档,无论是在类转储, UIDevice 的https://github.com/kennytm/iphone-private-frameworks/blob/master/UIKit/UIDevice2.h\">私人标题类。
I haven't found any method for this in the official documentation, neither in the class-dumped, private header of the UIDevice
class.
所以我们必须想出一些东西。我目前考虑的最好的解决方案类似于估计下载时间时所采用的方法:计算下载/充电的平均速度,并将剩余的(数据或费用)量除以该速度:
So we have to come up with something. The best "solution" I have in mind at the moment is similar to the approach taken when estimating download time: calculating an average speed of download/charging, and dividing the remaining amount (of data or charge) by that speed:
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
float prevBatteryLev = [UIDevice currentDevice].batteryLevel;
NSDate *startDate = [NSDate date];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(batteryCharged)
name:UIDeviceBatteryLevelDidChangeNotification
object:nil
];
- (void)batteryCharged
{
float currBatteryLev = [UIDevice currentDevice].batteryLevel;
// calculate speed of chargement
float avgChgSpeed = (prevBatteryLev - currBatteryLev) / [startDate timeIntervalSinceNow];
// get how much the battery needs to be charged yet
float remBatteryLev = 1.0 - currBatteryLev;
// divide the two to obtain the remaining charge time
NSTimeInterval remSeconds = remBatteryLev / avgChgSpeed;
// convert/format `remSeconds' as appropriate
}
这篇关于剩余时间,直到充电完成,iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!