我在监控电池方面遇到了问题。在前台我只能获得一次级别,似乎我也没有在后台获得更改。
如果我在一个监视器前放置一个新的UIViewController,然后再次返回,则更新电池电量。
-(void)startMonitoringBattery
{
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(batteryStateDidChange:)
name:UIDeviceBatteryStateDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(batteryLevelDidChange:)
name:UIDeviceBatteryLevelDidChangeNotification
object:nil];
batteryCheckTimer = [NSTimer scheduledTimerWithTimeInterval:(10.0)
target:self
selector:@selector(batteryCheck)
userInfo:nil
repeats:YES];
}
- (void)batteryStateDidChange:(NSNotification *)notification
{
[self batteryCheck];
}
- (void)batteryLevelDidChange:(NSNotification *)notification
{
[self batteryCheck];
}
当调用此方法时,我想获得当前的电池电量,而不是我开始监控时的电池电量。
- (void)batteryCheck
{
float STOP_AT_BATTERY_LEVEL = 50.0;
float currentBatteryLevel = [UIDevice currentDevice].batteryLevel;
if([UIDevice currentDevice].batteryLevel <= STOP_AT_BATTERY_LEVEL)
{
// STOP BATTERY CONSUMING TASK
}
}
问题: