0

バッテリーの監視に問題があります。フォアグラウンドではレベルを一度しか取得できず、バックグラウンドでも変化が得られないようです。 

1 つの監視の前に新しい 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
     }
}

質問:

  1. batteryStateDidChange と batteryLevelDidChange が複数回呼び出されないのはなぜですか?
  2. batteryMonitoringEnabled を true に設定しても、[UIDevice currentDevice].batteryLevel から現在のバッテリー レベルをタイマーで取得できないのはなぜですか?
  3. アプリがバックグラウンドにあるとき、または電話がロックされているときにバッテリーを監視することはできますか? GPS データを常に記録しているため、アプリはバックグラウンドで実行されています。
4

0 に答える 0