iOS 计算电池充满时间

        在viewDidLoad方法中添加如下代码:

[UIDevice currentDevice].batteryMonitoringEnabled = YES;
    self.prevBatteryLev = [UIDevice currentDevice].batteryLevel;
    self.startDate = [NSDate date];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryCharged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
         在ViewController.m 中添加batteryCharged:方法:

-(void)batteryCharged:(NSNotification *)note
{
    NSLog(@"%@",note);
    float currBatteryLev = [UIDevice currentDevice].batteryLevel;
    float avgChgSpeed = (self.prevBatteryLev - currBatteryLev) / [self.startDate timeIntervalSinceNow];
    float remBatteryLev = 1.0 - currBatteryLev;
    NSTimeInterval remSeconds = remBatteryLev / avgChgSpeed;
    NSLog(@"%f",remSeconds);
}


你可能感兴趣的:(ios,uidevice)