【问题标题】:Is there any method for getting the correct battery level in ios7?有什么方法可以在 ios7 中获得正确的电池电量?
【发布时间】:2013-11-28 08:56:52
【问题描述】:

我在一个名为“batteryLevel”的标签中显示了电池电量。用于此的代码如下,

batteryLevel.text = [NSString stringWithFormat:@"%0.0f%%", [UIDevice currentDevice].batteryLevel * 100]; 

但是我得到了 95%、90% 等的电池电量。我必须准确地显示电池电量,就像在当前设备中显示的那样。有没有正确的方法可以在 ios7 和以前的 ios 中找出电池电量版本?请给我一个解决这个问题的方法。

【问题讨论】:

    标签: iphone ios7 batterylevel


    【解决方案1】:

    最适合您的解决方案应该始终使用您使用计时器显示的值来提供外观的准确性。

    使用 batteryLevel 属性:

     [UIDevice currentDevice].batteryLevel.
    

    更多细节检查: https://developer.apple.com/Library/ios/samplecode/BatteryStatus/Introduction/Intro.html

    从上面的链接示例代码最新的1.2版也使用了batteryLevel属性。

    如果您使用的是越狱设备,则可以使用

    int percent = [(SBUIController*)[objc_getClass("SBUIController") sharedInstance] displayBatteryCapacityAsPercentage];
    

    此方法存在于 iOS 4.3+ 上,并且可能是 Battery Doctor 正在使用的。

    【讨论】:

    • 您能详细说明一下吗?他正在使用相同的[UIDevice currentDevice].batteryLevel
    • @TheTiger 他想要准确的更好水平,我给了他那个解决方案。
    • @Dhaval Bhadania:我检查了苹果示例代码。但我仍然得到电池电量是 5 的倍数。我想要设备中的确切电池电量,例如 99%、98%、92%等请给我一个解决方案。
    • 查看此链接:blog.coriolis.ch/2009/02/14/… 但请注意,苹果可能会拒绝此框架。最好使用 UIDevice 电池监控方法。
    • 那篇博文不适用于 iOS 7。不过在此之前它运行良好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多