iOS开发- 获取精确剩余电量
来源:互联网 发布:虎太郎斯文版mac 编辑:程序博客网 时间:2024/04/29 04:09
- [UIDevice currentDevice].batteryMonitoringEnabled = YES;
- double deviceLevel = [UIDevice currentDevice].batteryLevel;
获取当前剩余电量, 我们通常采用上述方法。这也是苹果官方文档提供的。
它返回的是0.00-1.00之间的浮点值。 另外, -1.00表示模拟器。
貌似这个方法不错, 也很简单。
但是仔细观察它的返回值, 我们可以发现。 它是以0.05递变的。 折算成100% 也就是以5%来递变的。
也就是说, 这个办法是存在缺陷的, 最起码, 它不精确。
后来找到了一个方法。相当精确, 误差保持在1%以内。
我们知道, Mac下有个IOKit.framework库。 它可以计算出我们需要的电量。
如果我们要使用它的话, (iOS是不提供的) 可以先建立一个Mac下的工程, 找到IOKit.framework,那IOKit.framework里面的IOPowerSources.h和IOPSKeys.h拷贝到你的iOS项目中。另外, 还需要把IOKit也导入到你的工程中去。
(当然, 如果嫌麻烦, 可以直接到我的Github中去下载工程, 导出需要的文件)
先给出我的Github下载链接:https://github.com/colin1994/batteryLevelTest.git
下面具体介绍下使用方法。
1。导入需要的IOPowerSources.h, IOPSKeys.h 和 IOKit
2。在实现的地方声明头文件
- #import "IOPSKeys.h"
- #import "IOPowerSources.h"
3。添加方法
- /**
- * Calculating the remaining energy
- *
- * @return Current batterylevel
- */
- -(double)getCurrentBatteryLevel
- {
- //Returns a blob of Power Source information in an opaque CFTypeRef.
- CFTypeRef blob = IOPSCopyPowerSourcesInfo();
- //Returns a CFArray of Power Source handles, each of type CFTypeRef.
- CFArrayRef sources = IOPSCopyPowerSourcesList(blob);
- CFDictionaryRef pSource = NULL;
- const void *psValue;
- //Returns the number of values currently in an array.
- int numOfSources = CFArrayGetCount(sources);
- //Error in CFArrayGetCount
- if (numOfSources == 0)
- {
- NSLog(@"Error in CFArrayGetCount");
- return -1.0f;
- }
- //Calculating the remaining energy
- for (int i = 0 ; i < numOfSources ; i++)
- {
- //Returns a CFDictionary with readable information about the specific power source.
- pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
- if (!pSource)
- {
- NSLog(@"Error in IOPSGetPowerSourceDescription");
- return -1.0f;
- }
- psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));
- int curCapacity = 0;
- int maxCapacity = 0;
- double percent;
- psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
- CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);
- psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
- CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);
- percent = ((double)curCapacity/(double)maxCapacity * 100.0f);
- return percent;
- }
- return -1.0f;
- }
4。调用方法
- NSLog(@"%.2f", [self getCurrentBatteryLevel]);
0 0
- iOS开发- 获取精确剩余电量
- iOS开发- 获取精确剩余电量
- iOS开发- 获取精确剩余电量
- iOS 获取剩余电量
- iOS开发-获取设备电量
- 获取手机电池的剩余电量
- 获取ios设备电量
- 获取ios设备电量
- c#,使用WMI对象获取笔记本电池剩余电量的百分比
- 剩余电量测量
- android查看剩余电量
- 获取 ios 系统网络状况、电量
- 获取 ios 系统网络状况、电量
- IOS 获取,如电量,CPU,型号等
- IOS 获取,如电量,CPU,型号等
- IOS 获取,如电量,CPU,型号
- iOS获取设备电量、内存、容量
- Unity IOS获取电量和网络类型
- Java多线程
- Java 8 新特性
- codeforces 339A Helpful Maths(greedy implementation sortings strings水题)
- 【LeetCode】Palindrome Number
- 【java】保护性拷贝的重要性
- iOS开发- 获取精确剩余电量
- HDOJ1172 猜数字 【暴力枚举】
- 彻底解决Operation not allowed after ResultSet closed的问题
- Serializable接口
- android GC内存泄露问题
- TCP协议三次握手过程分析
- 黑马程序员 交通灯管理系统
- 数字调节控件实现
- Java to C# Converter