Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
670 views
in Technique[技术] by (71.8m points)

objective c - Determine accurate iPhone battery level

I'm trying to get the battery level/state as follows:

- (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%%
%@", [[UIDevice currentDevice] batteryLevel] * 100,[batteryStatus objectAtIndex:[[UIDevice currentDevice] batteryState]] ];
        [textViewStatus setText:msg];
        NSLog(@"%@", msg);
    }
}

However, I am not getting changes in the battery level, and the values are not exact.

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

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Although I can't see anything specifically wrong with your code, please try the following

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

Also, it's worth noting that in my experience the notifications only ever fire at 5% intervals i.e when the battery level changes from 100% to 95% and 95% to 90% etc


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...