简体   繁体   English

如何在iOS中以1%的准确度获取电池电量

[英]How to get battery level with 1 % accuracy in ios

How to get battery level with 1 % accuracy . 如何以1%的精度获得电池电量。

       [UIDevice currentDevice].batteryLevel

Will give battery level with accuracy of 5%. 将给电池电量以5%的精度。 But i recently used Battery Doctor App on my iphone 4S running ios 5.1, which is giving battery level with 1% accuracy . 但是我最近在运行ios 5.1的iphone 4S上使用了Battery Doctor App,它给电池电量提供了1%的准确度。 Anyone has any idea , how can we get that accuracy ... I have tried and searched a lot but not getting how they are calculating it .. Thanks in advance... 任何人都有任何想法,我们如何才能获得该准确性...我已经尝试并进行了很多搜索,但没有获得他们如何计算它的准确性..在此先感谢...

Starting with iOS 8, 从iOS 8开始,

[UIDevice currentDevice].batteryLevel

provides 1% accuracy :) 提供1%的精度:)

如果关闭设置->常规->用法->电池百分比,电池医生将以%5的精度显示您的电池电量...因此,我猜他们正在为设备的右上角运行某种图像识别算法,其中显示电池百分比。

If you're jailbroken, you can simply use 如果您越狱了,可以简单地使用

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

This method is present on iOS 4.3+, and is probably what Battery Doctor is using. 此方法在iOS 4.3+上存在,并且可能是Battery Doctor正在使用的方法。

Answer comes from this question : 答案来自这个问题

You can use an NSTimer to manipulate the values you show giving the appearance of accuracy. 您可以使用NSTimer来操纵你展示给准确的外观值。 For each session the app is active, make a sample set of battery data(like the time required for battery level to fall from 100% to 95%, then 95% to 90%, and so on) and save it on the device itself. 对于每个活动的应用程序,请制作一组电池数据示例(例如电池电量从100%下降到95%,然后从95%下降到90%等),然后将其保存在设备本身上。 Then, use the timer to set off updated(and estimated) battery value according to the time required for the battery level to fall 5% divided by 5(assuming each percentage falls after the same interval, which will vary, but it won't be a noticeable deviation). 然后,使用计时器根据电池电量下降5%所需时间除以5(假设每个百分比在相同的间隔后下降,这有所不同,但不会改变)来更新(和估算)电池值是明显的偏差)。 It's true that battery life falls with time but since we adjust the timer value according to the values saved in the last session, it should continue being pretty much accurate over the life of the device. 的确,电池寿命会随着时间而下降,但是由于我们根据上次会话中保存的值来调整计时器的值,因此在设备的整个使用寿命中,它应该继续保持非常准确的状态。

Edit- For the initial state, you can provide a default value depending on the iPhone model(3,3G,4,4S). 编辑-对于初始状态,您可以根据iPhone型号(3,3G,4,4S)提供默认值。 Different values can be calculated for these different devices. 可以为这些不同的设备计算不同的值。

In iOS 8.1.x I have noticed that battery level is now reporting the exact battery level for apps now. 在iOS 8.1.x中,我注意到电池电量现在正在报告应用程序的确切电池电量。 Previously, UIDevice would report every 5%. 以前, UIDevice将每5%报告一次。 There is nothing more for you to do in iOS 8.1.x as [UIDevice currentDevice].batteryLevel should report this automatically now. 在iOS 8.1.x中,您无需执行任何其他操作,因为[UIDevice currentDevice].batteryLevel应该会立即自动报告此情况。

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

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