2011-06-27 13 views
8

Próbuję uzyskać baterii poziom/stan następująco:Określ poziom dokładne baterii iPhone

- (void) viewWillAppear: (BOOL) animated{ 

    [self viewWillAppear:animated]; 

    // Battery state 
    [self batteryStatus]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStatus) name:UIDeviceBatteryLevelDidChangeNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStatus) name:UIDeviceBatteryStateDidChangeNotification object:nil]; 


    } 

- (void)batteryStatus 
{ 
    NSArray *batteryStatus = [NSArray arrayWithObjects: 
           @"Battery status is unknown.", 
           @"Battery is in use (discharging).", 
           @"Battery is charging.", 
           @"Battery is fully charged.", nil]; 
    if ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateUnknown) 
    { 
     [textViewStatus setText:[batteryStatus objectAtIndex:0]]; 
     NSLog(@"%@", [batteryStatus objectAtIndex:0]); 
    } 
    else 
    { 
     NSString *msg = [NSString stringWithFormat:@"Battery charge level: %0.2f%%\n%@", [[UIDevice currentDevice] batteryLevel] * 100,[batteryStatus objectAtIndex:[[UIDevice currentDevice] batteryState]] ]; 
     [textViewStatus setText:msg]; 
     NSLog(@"%@", msg); 
    } 
} 

Jednak ja nie otrzymuję zmiany poziomu baterii, a wartości nie są dokładne.

- (void) viewWillAppear:(BOOL)animated { 
    UIDevice *device = [UIDevice currentDevice]; 
    device.batteryMonitoringEnabled = YES; 
    textViewStatus.text = [NSString stringWithFormat:@"%.2f", device.batteryLevel]; 
    [device addObserver:self forKeyPath:@"batteryLevel" options:0x0 context:nil]; 
    [super viewWillAppear:animated]; 
} 

- (void) viewDidDisappear:(BOOL)animated { 
    UIDevice *device = [UIDevice currentDevice]; 
    device.batteryMonitoringEnabled = NO; 
    [device removeObserver:self forKeyPath:@"batteryLevel"]; 
    [super viewDidDisappear:animated]; 
} 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    UIDevice *device = [UIDevice currentDevice]; 
    if ([object isEqual:device] && [keyPath isEqual:@"batteryLevel"]) { 
     NSLog(@"Battery level :%f",device.batteryLevel); 
     textViewStatus.text = [NSString stringWithFormat:@"%.2f", device.batteryLevel]; 
    } 
} 

Wszelkie pomysły?

Odpowiedz

21

Chociaż nie widzę nic specjalnie złego w kodzie, spróbuj wykonać następujące

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIDevice *device = [UIDevice currentDevice]; 
    device.batteryMonitoringEnabled = YES; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:device]; 
} 

- (void)batteryChanged:(NSNotification *)notification 
{ 
    UIDevice *device = [UIDevice currentDevice]; 
    NSLog(@"State: %i Charge: %f", device.batteryState, device.batteryLevel); 
} 

Ponadto, warto zauważyć, że w moim doświadczeniu notyfikacje tylko kiedykolwiek pożar w 5% odstępach tj gdy bateria zmiany poziomu od 100% do 95% i od 95% do 90% itp.

+0

Dzięki za odpowiedź ur. Tak, pokazuje zmiany w odstępach 5%. Ale chcę pokazać zmiany na każdym poziomie. –

+0

@sandhya Jest to ograniczenie zestawu SDK i sposobu, w jaki Apple zdecydowało się go zaprojektować. Zgodnie z dokumentacją firmy Apple ... 'Powiadomienia o zmianie poziomu naładowania baterii są wysyłane nie częściej niż raz na minutę" –

+0

, gdy aplikacja jest zamknięta, czy możemy użyć tych powiadomień? nie jestem pewien, dlaczego pytasz ... – Apple

Powiązane problemy