剩余时间直到充电完成,iOS

时间:2012-12-16 07:02:01

标签: iphone ios ipad cocoa-touch

我正在使用:

UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
float batLeft = [myDevice batteryLevel];
int i = [myDevice batteryState];    
int batinfo = batLeft * 100;

查找电池状态。 我期待找到,如何找到剩余的时间,直到充电完成。 例如:剩余1小时20分钟。 我怎样才能以编程方式找到它?

1 个答案:

答案 0 :(得分:8)

我在official documentation中找不到任何方法,在UIDevice类的类转储private header中也没有找到。

所以我们必须提出一些建议。我现在想到的最好的“解决方案”类似于估算下载时间时采用的方法:计算下载/计费的平均速度,并将剩余的数据(数据或费用)除以该速度:

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