【问题标题】:How to get real time battery level on iOS with a 1% granularity如何以 1% 的粒度在 iOS 上获取实时电池电量
【发布时间】:2012-08-04 09:00:24
【问题描述】:

我正在使用这个函数来获取设备的当前电池电量:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
UIDevice *myDevice = [UIDevice currentDevice];

[myDevice setBatteryMonitoringEnabled:YES];
double batLeft = (float)[myDevice batteryLevel]; 
NSLog(@"%f",batLeft);

但结果的粒度为 5%。示例:当手机电池电量为 88% 时,它仅记录 0.85 的值。 batteryLevel 只返回以 0.05 为增量的值。例如:0.85、0.9、0.95,并且从不返回 0.82 或 0.83 之类的值。

有没有什么办法可以得到更高精度的百分比?

【问题讨论】:

  • 记录一下:我只是用 iPhone 6 Plus 检查了您的代码,它确实显示了所有标量级别,例如 0.49 等

标签: iphone cocoa-touch battery batterylevel


【解决方案1】:

至少有四种不同的方式来读取电池电量,所有四种方式都可能返回不同的值。

这是这些值随时间变化的图表。

这些值是用这个 iOS 项目记录的:https://github.com/nst/BatteryChart

请查看代码以供参考。

【讨论】:

  • 此应用是否仍在后台运行并报告百分比?或者你只是让应用程序运行?
【解决方案2】:

看看这个网站: Reading the battery level programmatically

但是,小心使用。此处使用的所有 API 在 iPhone 上均未记录,如果您将此应用程序提交到 App Store,可能会导致拒绝。虽然电池充电状态不准确,但我建议使用 UIDevice 电池监控方法。

【讨论】:

  • 感谢您的链接。我已经检查过了。我看到了这一行:“不要忘记在发货前从您的代码中删除标头和 libIOKit.A.dylib!”,这是否意味着完成后,我可以删除 libIOKit.A.dylib 并从我的代码中删除标头上传到 Apple Store?
【解决方案3】:
UIDevice *myDevice = [UIDevice currentDevice];    
[myDevice setBatteryMonitoringEnabled:YES];

double batLeft = (float)[myDevice batteryLevel] * 100;
NSLog(@"%.f", batLeft);    

NSString * levelLabel = [NSString stringWithFormat:@"%.f%%", batLeft];    
lblLevel.text = levelLabel;

【讨论】:

  • 您的第一行是多余的,因为您在第三行代码中做了完全相同的事情。
  • @VictorEngel 我刚刚编辑它以删除重复的代码行。感谢您指出这一点。
【解决方案4】:

Swift版本获取电池电量:

UIDevice.current.isBatteryMonitoringEnabled = true
let batteryLevel = UIDevice.current.batteryLevel 

batteryLevel 返回 0,39;对我来说是 0,40 个值。

【讨论】:

  • 它不适合我吗?我需要执行任何其他配置吗?我得到 -1 作为答案。
  • 这段代码是你写的吗? UIDevice.current.isBatteryMonitoringEnabled = true 如果你没有写这个你会得到-1。
  • 是的,我写了这一行“UIDevice.current.isBatteryMonitoringEnabled = true”。另外,我是开发 iOS 应用程序的新手。我在 ViewController 下调用这个代码表单 viewDIdLoad() 方法。另外,我连接到模拟器,而不是实际的电话。这会有所作为吗?
  • 当然模拟器会返回“-1”。仅将此代码用于真实设备。
【解决方案5】:

上面的答案非常好,但它们都在 Obj-C 中,我已经将这些与其他示例一起用于在MonoTouch 上执行相同的任务,所以我将我的代码放在这里以防万一有人需要它:

try
{
    UIDevice.CurrentDevice.BatteryMonitoringEnabled = true;
    _Battery.Level = (int)(UIDevice.CurrentDevice.BatteryLevel * IOSBatteryLevelScalingFactor);
    _Battery.State = UIDevice.CurrentDevice.BatteryState;
}
catch (Exception e)
{
    ExceptionHandler.HandleException(e, "BatteryState.Update");
    throw new BatteryUpdateException();
}
finally
{
    UIDevice.CurrentDevice.BatteryMonitoringEnabled = false;
}

我的博客上还有一个完整的帖子,在here 中提供所有详细信息

【讨论】:

    【解决方案6】:

    你可以在这里找到完美的答案

    Xcode:11.4 斯威夫特:5.2

    Click Here

    【讨论】:

      【解决方案7】:

      如果您使用的是 swift 5.1 或更高版本,此代码肯定适合您。

      第 1 步:- 开始,首先启用当前设备的 isBatteryMonitoringEnabled 属性,如下所示:-

      UIDevice.current.isBatteryMonitoringEnabled = true
      

      第 2 步:- 您现在可以读取当前电池电量

      let level = UIDevice.current.batteryLevel
      print(level)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-29
        • 1970-01-01
        相关资源
        最近更新 更多