确定精确的iPhone电池电量 [英] Determine accurate iPhone battery level
本文介绍了确定精确的iPhone电池电量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图获取电池电量/状态如下:
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%%\n%@", [[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];
}
}
有任何想法吗?
推荐答案
虽然我无法看到您的代码有什么特别的错误,请尝试以下
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);
}
此外,值得注意的是,根据我的经验, %间隔,即当电池电量从100%变化到95%和95%到90%等时
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
这篇关于确定精确的iPhone电池电量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文