【问题标题】:How to get the exact battery level?如何获得准确的电池电量?
【发布时间】:2011-09-09 10:38:46
【问题描述】:

这是我的简单电池级别测试应用程序的简单代码。

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

这是我的应用程序的图像,希望你能看到状态栏电池电量和我的应用程序电池电量。我不知道为什么这些级别不同......? 一个是 99%,应用级别是 0.95,实际上是 95%

【问题讨论】:

    标签: ios swift battery


    【解决方案1】:
    // Enable this property
    UIDevice.current.isBatteryMonitoringEnabled = true
    
    // Then add observer
    NotificationCenter.default.addObserver(self, selector: #selector(batteryLevelDidChange(notification:)), name: NSNotification.Name.UIDeviceBatteryLevelDidChange, object: nil)
    

    观察者法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      相关资源
      最近更新 更多