【问题标题】:battery consumption check for my app in iOS在 iOS 中检查我的应用的电池消耗
【发布时间】:2016-10-17 12:33:09
【问题描述】:

我需要获取我的应用的电池消耗详细信息。
我使用仪器进行跟踪,我的能源使用水平为 1/20。
这是什么 1/20?

【问题讨论】:

  • 手机电量是否为 5%?
  • @KSigWyatt 电池电量为 100%

标签: ios xcode instruments


【解决方案1】:
  1. 很快:

    UIDevice *Device = [UIDevice currentDevice];
    [Device setBatteryMonitoringEnabled:YES];
    
     int state = [Device batteryState];
    NSLog(@"Now the status: %d",state);
     double batLeft = (float)[Device batteryLevel] * 100;
      NSLog(@"Charge left: %ld", batLeft);
    
  2. 该 API 允许您注册以接收有关电池电量更改的通知。它仅以 5% 的增量向上或向下报告更改,但您可以使用计时器并测量两次更改(或初始电池电量和第一次更改)之间的时间。以下是您注册通知的方式:

       // Use this call to get the current battery level as a float
      // [[UIDevice currentDevice] batteryLevel]
    
       [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
    
      [[NSNotificationCenter defaultCenter] addObserver:self
                                        selector:@selector(batteryStateDidChange:)
                                         name:UIDeviceBatteryStateDidChangeNotification
                                       object:nil];
    
       [[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(batteryLevelDidChange:)
                                         name:UIDeviceBatteryLevelDidChangeNotification
                                       object:nil];
    

第一个通知告诉您当前状态,例如拔下、充电或充满。每当达到 5% 的增量时,都会触发第二个。

在我看来,如果你得到的只是 5% 的变化通知,那么准确度就不是你可以很好或快速计算出来的东西。如果设备什么都不做,5% 的变化可能需要很长时间。

也许您可以使用计时器监控 [[UIDevice currentDevice] batteryLevel],但是,虽然我没有尝试过,但我认为它只会以相同的 5% 增量更新。

发件人:iphone: Calculating battery life

  1. 这是一个很好的例子:http://blog.coriolis.ch/2009/02/14/reading-the-battery-level-programmatically/

【讨论】:

  • 我不是在询问我手机的电池电量。我想计算用户在 iPhone 上使用我的应用时的电池消耗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-16
相关资源
最近更新 更多