首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >确定准确的iPhone电池电量

确定准确的iPhone电池电量
EN

Stack Overflow用户
提问于 2011-06-27 15:53:09
回答 1查看 7.3K关注 0票数 8

我正在尝试获取电池电量/状态,如下所示:

代码语言:javascript
复制
- (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);
    }
}

然而,我没有得到电池电量的变化,而且数值也不准确。

代码语言:javascript
复制
- (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];
    }
}

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-27 16:40:04

尽管我看不到您的代码有任何特别的错误,但请尝试执行以下操作

代码语言:javascript
复制
- (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);
}

此外,值得注意的是,根据我的经验,只有在电池电量从100%到95%和95%到90%等情况下,通知才会以5%的间隔发出

票数 22
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6490010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档