iPhone:当电池电量非常低时收到警告 [英] iPhone: Get warning when battery power is very low

查看:261
本文介绍了iPhone:当电池电量非常低时收到警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道当设备电池​​电量非常低时,如何在我的应用程序委派中收到警告。

解决方案



您可以使用 UIDevice 的电池电量属性。如果电池电量低于5%,则显示警报。您可以定期轮询应用程式委托中的电池电量。

  UIDevice * myDevice = [UIDevice currentDevice]; 
[myDevice setBatteryMonitoringEnabled:YES];
float batteryLevel = [myDevice batteryLevel];

文档


batteryLevel

设备的电池电量。 (只读)



@property(nonatomic,readonly)float batteryLevel



讨论

电池电量范围从0.0(完全放电)到1.0
(100%充电)。在访问此属性之前,请确保电池
监控已启用。



如果未启用电池监控,电池状态为
UIDeviceBatteryStateUnknown ,此属性的值为-1.0。



可用性

另请参阅

@property batteryState

@property batteryMonitoringEnabled



已声明

UIDevice.h



I want to know how can i get warning in my app delegate when the device battery power is very low. So that i can pause the running game.

Any idea?

解决方案

You could use the battery level property from UIDevice. If the battery level is less than 5% show an alert for example. You could poll periodically for the battery level in your app delegate for example.

UIDevice *myDevice = [UIDevice currentDevice]; 
[myDevice setBatteryMonitoringEnabled:YES]; 
float batteryLevel = [myDevice batteryLevel];

Explanation from the docs:

batteryLevel
The battery charge level for the device. (read-only)

@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

这篇关于iPhone:当电池电量非常低时收到警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆