How to get battery level with 1 % accuracy in ios

冷暖自知 提交于 2019-11-29 06:10:56
Nandan Sawant

Starting with iOS 8,

[UIDevice currentDevice].batteryLevel

provides 1% accuracy :)

If you turn off Setting->General->Usage->Battery Percentage, Battery Doctor shows your battery level with %5 precision...So I guess that they are running some image recognition algorithm for the upper right corner of the device, where the battery percent is shown.

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.

tipycalFlow

Answer comes from this question:

You can use an NSTimer to manipulate the values you show giving the appearance of accuracy. 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. 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). 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). 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. Previously, UIDevice would report every 5%. There is nothing more for you to do in iOS 8.1.x as [UIDevice currentDevice].batteryLevel should report this automatically now.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!