我想要检测iPhone上电池电量不足警报出现的确切时间。他们似乎出现了20%和10%的电池水平的数量。
我不能使用ApplicationWillResignActive/DidEnterBackground,,因为我需要能够将这些电量不足的警报与其他可能导致调用ApplicationWillResignActive/DidEnterBackground的事件区分开来。这是程序的一个要求。
我不能使用UIDeviceBatteryLevelDidChangeNotification,因为即使这些--理应--每5%的变化就会发生……似乎在实践中,它们有时会比当时的电池读数低几个百分点,因此在电池电量不足时不会发生。请看这里(Battery Monitoring updates at uneven intervals)。
有没有办法在这些电量不足的警报发生时准确地检测到它们,并将它们与其他应用程序状态更改分开处理?
谢谢你们!!
发布于 2013-11-20 12:50:27
您可以使用UIDevice中的电池电量属性。例如,如果电池电量低于5%,则显示警报。例如,你可以在你的应用程序代理中定期共享电池电量。
UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
float batteryLevel = [myDevice batteryLevel];
和文档中的解释:
batteryLevel
The battery charge level for the device.
@property (nonatomic, readonly) float batteryLevel
Discussion
Battery level ranges from 0.0 (fully discharged) to 1.0 (100% charged). Before accessing this property, ensure that battery monitoring is enabled.
If battery monitoring is not enabled, battery state is UIDeviceBatteryStateUnknown and the value of this property is –1.0.
Availability
Available in iOS 3.0 and later.
See Also
@property batteryState
@property batteryMonitoringEnabled
Declared In
UIDevice.h
https://stackoverflow.com/questions/20087505
复制相似问题