iOS电池警报检测

时间:2013-11-20 04:42:01

标签: ios alert detection battery

我想确切地检测出iPhone上的低电量警报何时出现。它们似乎达到了20%和10%的电池电量。

我不能使用ApplicationWillResignActive / DidEnterBackground,因为我需要能够将这些低电量警报与可能导致调用ApplicationWillResignActive / DidEnterBackground的其他事物区分开来。这是该计划的要求。

我不能使用UIDeviceBatteryLevelDidChangeNotification,因为即使这些都是 - 在每5%的变化时发生 - 似乎在实践中它们有时会从电池读数中减去几个百分点,因此不会发生发生低电量警报时。见这里(Battery Monitoring updates at uneven intervals)。

有没有办法在这些低电量警报出现时准确地检测这些低电量警报并将其与其他应用程序状态变化分开处理?

谢谢你们!

1 个答案:

答案 0 :(得分:0)

您可以使用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