time remaining until charge is complete,iOS

后端 未结 1 1331
野性不改
野性不改 2020-12-14 09:16

I am using :

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


        
相关标签:
1条回答
  • 2020-12-14 10:03

    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
    }
    
    0 讨论(0)
提交回复
热议问题