我试图正确获取电池电量。但是这个价值并不像iPhone的状态栏中的价值。iOS:如何正确获取电池电量
通过代码使用的UIDevice:
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
[[UIDevice currentDevice] batteryLevel];
请帮助我的人!我需要正确的电池电量,就像在状态栏imdimatery。
我试图正确获取电池电量。但是这个价值并不像iPhone的状态栏中的价值。iOS:如何正确获取电池电量
通过代码使用的UIDevice:
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
[[UIDevice currentDevice] batteryLevel];
请帮助我的人!我需要正确的电池电量,就像在状态栏imdimatery。
井苹果文档说此:
电池水平从0.0的范围(完全放电)到1.0(100%充电)。在访问此属性之前,请确保电池监控已启用。
所以,你的代码应该是这样的:
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
float batteryLevel = [[UIDevice currentDevice] batteryLevel];
//This will give you the battery between 0.0 (empty) and 1.0 (100% charged)
//If you want it as a percentage, you can do this:
batteryLevel *= 100;
希望这有助于!
您是否真的阅读过文档? http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instp/UIDevice/batteryLevel – Luke 2012-08-03 19:19:21
可能的dumplicate与http://stackoverflow.com/questions/11807295/how-to-get-real-time-battery-level-on-ios – cat 2013-05-28 02:56:56