2011-06-27 16 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% से 9 0% आदि

+0

आपकी प्रतिक्रिया के लिए धन्यवाद। हां यह 5% अंतराल पर परिवर्तन दिखा रहा है। लेकिन मैं हर स्तर पर बदलाव दिखाना चाहता हूं। –

+0

@ संध्या यह एसडीके की एक सीमा है और जिस तरह से ऐप्पल ने इसे इंजीनियर करने का फैसला किया है। ऐप्पल के दस्तावेज़ों के अनुसार ... 'बैटरी स्तर परिवर्तन के लिए अधिसूचनाएं प्रति मिनट एक बार से अधिक बार नहीं भेजी जाती हैं' –

+0

जब ऐप बंद होता है तो क्या हम इन अधिसूचनाओं का उपयोग कर सकते हैं? मुझे यकीन नहीं है कि तुमसे क्यों पूछते हैं .. – Apple

संबंधित मुद्दे