iPhone: Get warning when battery power is very low(iPhone:电池电量非常低时收到警告)
问题描述
我想知道当设备电池电量非常低时如何在我的应用程序委托中收到警告.这样我就可以暂停正在运行的游戏.
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.
有什么想法吗?
推荐答案
您可以使用 UIDevice
中的电池电量属性.例如,如果电池电量低于 5%,则显示警报.例如,您可以在应用委托中定期轮询电池电量.
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];
来自 文档:
电池电量
设备的电池电量.(只读)
batteryLevel
The battery charge level for the device. (read-only)
@property(nonatomic, readonly) float batteryLevel
讨论
电池电量范围从 0.0(完全放电)到 1.0(100% 收费).在访问此属性之前,请确保电池监控已启用.
Discussion
Battery level ranges from 0.0 (fully discharged) to 1.0
(100% charged). Before accessing this property, ensure that battery
monitoring is enabled.
如果未启用电池监控,则电池状态为UIDeviceBatteryStateUnknown
,该属性的值为–1.0.
If battery monitoring is not enabled, battery state is
UIDeviceBatteryStateUnknown
and the value of this property is –1.0.
可用性
适用于 iOS 3.0 及更高版本.
Availability
Available in iOS 3.0 and later.
另请参阅@property batteryState
@property batteryMonitoringEnabled
声明于
UIDevice.h
Declared In
UIDevice.h
这篇关于iPhone:电池电量非常低时收到警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iPhone:电池电量非常低时收到警告
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01