我正在监控电池事件UIDeviceBatteryLevelDidChangeNotification
和UIDeviceBatteryStateDidChangeNotification
。我正在寻找 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.");
}
}