How to get battery level with 1

2019-01-25 07:44发布

问题:

How to get battery level with 1 % accuracy .

       [UIDevice currentDevice].batteryLevel

Will give battery level with accuracy of 5%. But i recently used Battery Doctor App on my iphone 4S running ios 5.1, which is giving battery level with 1% accuracy . 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...

回答1:

Starting with iOS 8,

[UIDevice currentDevice].batteryLevel

provides 1% accuracy :)



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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.



标签: ios battery