android:靠近脸部时关闭屏幕
我的应用程序允许用户访问他们的公司语音邮件。通常,在通话过程中,当用户将设备放在耳边时,屏幕会关闭,这样他们就不会意外地用脸按下按钮。我想让我的应用程序在用户收听语音邮件时执行相同的操作。
有人知道该怎么做吗?
My app allows the user to access their corporate voice mail. Normally, durring a phone call when the user holds the device up to their ear, the screen shuts off so they wont accidentally push buttons with their face. I would like to make my app do the same thing when the user is listening to their voice mail.
anyone know how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果允许您查看开源代码而不会给自己带来问题,请检查 的源代码Android 手机应用程序。具体来说 src/com/android/phone/PhoneApp.java 和 src/com/android/phone/InCallScreen.java。
来自 src/com/android/phone/PhoneApp.java:
此外,如果您查看 PowerManager 类的代码,PROXIMITY_SCREEN_OFF_WAKE_LOCK 已记录(但隐藏)并且应该执行您想要的操作(我不确定这适用于哪个 API 级别) ,但是)——但由于某种原因不在表中。
如果您不害怕使用潜在的未记录功能,那么它应该完全满足您的需要。
If you are allowed to look at open source code without causing yourself problems, check the source of the Android Phone Application. Specifically src/com/android/phone/PhoneApp.java and src/com/android/phone/InCallScreen.java.
From src/com/android/phone/PhoneApp.java:
Additionally, if you look at the code for the PowerManager class, PROXIMITY_SCREEN_OFF_WAKE_LOCK is documented (but hidden) and should do what you want ( I am not sure which API level this works for, however ) -- but not in the table for some reason.
If you aren't afraid of using a potential undocumented feature, it should do exactly what you need.
从 API 级别 21 (Lollipop) 开始,您可以获得邻近唤醒锁,就像这样:
然后由您来获取和释放锁。
PS:
PowerManager#getSupportedWakeLockFlags
曾被隐藏,但现在不再存在。他们发明了isWakeLockLevelSupported
。as of API level 21 (Lollipop) you can get proximity wake lock this just like that:
then it is up to you to acquire and release the lock.
PS:
PowerManager#getSupportedWakeLockFlags
was hidden, but now exists nomore. They have inventedisWakeLockLevelSupported
instead.您所看到的是接近传感器的使用。对于具有该功能的设备,您可以通过
SensorManager
访问它。What you are seeing is the use of a proximity sensor. For devices that have one, you access it through
SensorManager
.也许您不再需要它,但对于那些对代码感兴趣的人,您可以查看我的SpeakerProximity项目,网址为http://code.google.com/p/speakerproximity/
Probably you don't need it anymore but for the ones that are interested in code you could have a look at my SpeakerProximity project at http://code.google.com/p/speakerproximity/