电池监控以不均匀的间隔更新

时间:2012-10-17 03:09:35

标签: ios cocoa-touch uidevice batterylevel

我正在监控电池事件UIDeviceBatteryLevelDidChangeNotificationUIDeviceBatteryStateDidChangeNotification。我正在寻找 20% 10%,但是,我看到这些事件发生了23%,13%,他们应该击中 0.05 间隔或每 5%

问题

有没有其他人经历过这种情况,还是有更好的方法来确保水平更准确?

实施例

-(void)startMonitoringForBatteryChanges
{
    // Enable monitoring of battery status
    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

    // Request to be notified when battery charge or state changes
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkBatteryStatus) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkBatteryStatus) name:UIDeviceBatteryStateDidChangeNotification object:nil];
}

- (void)checkBatteryStatus
{
    float warningLevel = [[UIDevice currentDevice] batteryLevel] * 100;

    if ( warningLevel == 20.0 ) // 20%
    {
        NSLog(@"20% Warning.");
    }

    if ( warningLevel == 10.0 ) // 10%
    {
        NSLog(@"10% Warning.");
    }
}

0 个答案:

没有答案