2

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.");
    }
}
4

0 に答える 0