如何知道设备何时充电? [英] How to know when the device is charging?

查看:118
本文介绍了如何知道设备何时充电?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在设备充电时检测我的iOS应用?

How do I make my iOS app detect when the device is charging?

推荐答案

这是怎么回事。

UIDeviceBatteryState deviceBatteryState = [UIDevice currentDevice].batteryState;
if (deviceBatteryState == UIDeviceBatteryStateCharging || deviceBatteryState == UIDeviceBatteryStateFull) {
    // It is charging
}

来自苹果文档

UIDeviceBatteryState:
设备的电池电量状态。

UIDeviceBatteryState: The battery power state of the device.

typedef enum {
   UIDeviceBatteryStateUnknown,
   UIDeviceBatteryStateUnplugged,
   UIDeviceBatteryStateCharging,
   UIDeviceBatteryStateFull,
} UIDeviceBatteryState;

UIDeviceBatteryStateUnknown:
无法确定设备的电池状态。

UIDeviceBatteryStateUnknown: The battery state for the device cannot be determined.

UIDeviceBatteryStateUnplugged:
设备未插入电源;电池电量不足。

UIDeviceBatteryStateUnplugged: The device is not plugged into power; the battery is losing power.

UIDeviceBatteryStateCharging:
设备是插上电源,电池充电不到100%。

UIDeviceBatteryStateCharging: The device is plugged into power and the battery is less than 100% charged.

UIDeviceBatteryStateFull:
设备已接通电源,电池100%充电。

UIDeviceBatteryStateFull: The device is plugged into power and the battery is 100% charged.

更新:

完全回答你的问题。以下是检测充电器何时插入的方法。有一个名为 batteryMonitoringEnabled 的属性。

Just to answer your question completely. Here is how to detect when a charger is plugged in. There is a property named batteryMonitoringEnabled.

batteryMonitoringEnabled:一个布尔值,表示电池监控是否已启用(YES)是否(NO)

batteryMonitoringEnabled: A Boolean value indicating whether battery monitoring is enabled (YES) or not (NO).

如果您的应用需要通知电池状态变化,或者您想检查电池电量,请启用电池监控。

Enable battery monitoring if your app needs to be notified of changes to the battery state, or if you want to check the battery charge level.

此属性的默认值为,其中:

The default value of this property is NO, which:



  • 禁用与电池相关的通知的发布

  • 禁用读取电池电量和电池状态的能力

这将发布通知 UIDeviceBatteryLevelDidChangeNotification ,以及<电池电量变化时,code> UIDeviceBatteryStateDidChangeNotification 。您可以使用 UIDeviceBatteryStateDidChangeNotification 来检测充电器何时插入。

This will post the notifications UIDeviceBatteryLevelDidChangeNotification, and UIDeviceBatteryStateDidChangeNotification when the battery level changes. You can make use of UIDeviceBatteryStateDidChangeNotification to detect when the charger is plugged in.

这篇关于如何知道设备何时充电?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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