iPhone4如何判断电源线是否插入?

发布于 2024-12-12 11:38:34 字数 105 浏览 0 评论 0原文

我想知道我的应用程序是否在连接外部电源线的情况下运行。是否可以在运行时找出这个状态?

另一个问题:这是否能够区分真正的 USB 电源和那些外部“电池组”?

谢谢你!

I would like to know if my app is running with an external power cable attached. Is it possible to find out this state at runtime?

An extra question: would this be able to differentiate between true USB power and those external "battery packs"?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灯角 2024-12-19 11:38:34

使用 UIDevice 属性 batteryState

[[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateCharging

来自 UIDevice Docs

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

至于你的第二个问题。我不相信您可以确定电池组和壁式充电器之间的任何区别,因为上述 UIDeviceBatteryState 标志是设备电池可以报告的唯一“状态”。因此,电池组和壁式充电器都会显示为 UIDeviceBatteryStateChargingUIDeviceBatteryStateFull(如果电池组已插入但电量耗尽,则显示为 UIDeviceBatteryStateUnplugged) )。

Use UIDevice property batteryState:

[[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateCharging

From UIDevice Docs:

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

As for your 2nd question. I don't believe you can determine any difference between a battery pack and a wall charger since the above UIDeviceBatteryState flags are the only "states" a device battery can report. So both a battery pack and wall charger would appear as either UIDeviceBatteryStateCharging or UIDeviceBatteryStateFull (or UIDeviceBatteryStateUnplugged if the battery pack is plugged in but out of juice).

你是暖光i 2024-12-19 11:38:34

您可以检测电池是否正在充电,但这已经是现有 API 所能达到的最接近的结果了——可以这么说,无法检测电力“来自”何处。

UIDeviceBatteryState batteryState = [UIDevice currentDevice].batteryState;
if (batteryState == UIDeviceBatteryStateCharging) {
    // Your code here
}

You can detect whether the battery is charging, but that's as close as you can get with existing APIs – there's no way to detect where the power is "coming from", so to speak.

UIDeviceBatteryState batteryState = [UIDevice currentDevice].batteryState;
if (batteryState == UIDeviceBatteryStateCharging) {
    // Your code here
}
爱冒险 2024-12-19 11:38:34

我必须包括行

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

否则,batteryState 仅返回 UIDeviceBatteryStateUnknown。也许这是必要的,因为这个问题最初被提出并得到回答。我在这里找到了提示:确定准确的 iPhone 电池电量

I had to include the line,

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

or else batteryState only returns UIDeviceBatteryStateUnknown. Perhaps this became necessary since this question was initially asked and answered. I found the tip here: Determine accurate iPhone battery level.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文