2011-06-27 87 views
8

我試圖讓電池電量/狀態如下:確定準確的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); 
    } 
} 

在電池電量但是,我沒有得到改變,而值不準確。

- (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]; 
    } 
} 

任何想法?

回答

21

雖然我不能看到任何具體的你的代碼錯誤,請嘗試以下

- (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); 
} 

此外,值得注意的是,在我的經驗,通知永遠只火在5個%區間,即當電池級別從100%變爲95%和95%至90%等

+0

感謝您的回覆。是的,它以5%的間隔顯示變化。但是我想要展示每個級別的變化。 –

+0

@sandhya這是SDK的限制和Apple選擇設計它的方式。根據蘋果的文檔... '電池電量變化的通知不多於每分鐘一次# –

+0

當應用程序關閉,那麼我們可以使用這些通知嗎?我不確定這是爲什麼問你.. – Apple