iPhone4如何判断电源线是否插入?
我想知道我的应用程序是否在连接外部电源线的情况下运行。是否可以在运行时找出这个状态?
另一个问题:这是否能够区分真正的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
UIDevice
属性batteryState
:来自 UIDevice Docs:
至于你的第二个问题。我不相信您可以确定电池组和壁式充电器之间的任何区别,因为上述
UIDeviceBatteryState
标志是设备电池可以报告的唯一“状态”。因此,电池组和壁式充电器都会显示为UIDeviceBatteryStateCharging
或UIDeviceBatteryStateFull
(如果电池组已插入但电量耗尽,则显示为UIDeviceBatteryStateUnplugged
) )。Use
UIDevice
propertybatteryState
:From UIDevice Docs:
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 eitherUIDeviceBatteryStateCharging
orUIDeviceBatteryStateFull
(orUIDeviceBatteryStateUnplugged
if the battery pack is plugged in but out of juice).您可以检测电池是否正在充电,但这已经是现有 API 所能达到的最接近的结果了——可以这么说,无法检测电力“来自”何处。
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.
我必须包括行
[[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.