简体   繁体   English

如何获得准确的电池电量?

[英]How to get the exact battery level?

This is simple code of my simple batteryLevel Test app. 这是我简单的BatteryLevel Test应用程序的简单代码。

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIDevice *device = [UIDevice currentDevice];
    device.batteryMonitoringEnabled = YES;
    mLabel.text = [NSString stringWithFormat:@"%0.5f",device.batteryLevel];
}

This is the image of my app, hope you can see the status bar battery level and my application battery level. 这是我的应用程序的图像,希望您可以看到状态栏电池电量和我的应用程序电池电量。 I don't know why these levels are different....? 我不知道为什么这些水平不同...? one is 99% and apps level is 0.95 which is actually 95% 一个是99%,应用程序级别是0.95,实际上是95%

在此处输入图片说明

// Enable this property
UIDevice.current.isBatteryMonitoringEnabled = true

// Then add observer
NotificationCenter.default.addObserver(self, selector: #selector(batteryLevelDidChange(notification:)), name: NSNotification.Name.UIDeviceBatteryLevelDidChange, object: nil)

Observer method 观察者法

@objc func batteryLevelDidChange(notification: NSNotification) {
  // The battery's level did change
  print(UIDevice.current.batteryLevel)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM