android:靠近脸部时关闭屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3018716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android: turn off screen when close to face
提问by mtmurdock
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?
有人知道怎么做吗?
采纳答案by CommonsWare
What you are seeing is the use of a proximity sensor. For devices that have one, you access it through SensorManager
.
您所看到的是接近传感器的使用。对于有一个的设备,您可以通过 访问它SensorManager
。
回答by Dre
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.javaand src/com/android/phone/InCallScreen.java.
如果您可以查看开源代码而不会给自己带来问题,请检查Android Phone 应用程序的来源。特别是src/com/android/phone/PhoneApp.java和src/com/android/phone/InCallScreen.java。
From src/com/android/phone/PhoneApp.java:
来自 src/com/android/phone/PhoneApp.java:
//Around line 519
// Wake lock used to control proximity sensor behavior.
if ((pm.getSupportedWakeLockFlags()
& PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) != 0x0) {
mProximityWakeLock = pm.newWakeLock(
PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK,
LOG_TAG);
}
....
// Around line 1334
if (((state == Phone.State.OFFHOOK) || mBeginningCall)&& !screenOnImmediately) {
// Phone is in use! Arrange for the screen to turn off
// automatically when the sensor detects a close object.
if (!mProximityWakeLock.isHeld()) {
if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: acquiring...");
mProximityWakeLock.acquire();
} else {
if (VDBG) Log.d(LOG_TAG, "updateProximitySensorMode: lock already held.");
}
} else {
// Phone is either idle, or ringing. We don't want any
// special proximity sensor behavior in either case.
if (mProximityWakeLock.isHeld()) {
if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: releasing...");
// Wait until user has moved the phone away from his head if we are
// releasing due to the phone call ending.
// Qtherwise, turn screen on immediately
int flags =
(screenOnImmediately ? 0 : PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE);
mProximityWakeLock.release(flags);
}
}
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.
此外,如果您查看 PowerManager 类的代码,PROXIMITY_SCREEN_OFF_WAKE_LOCK 已记录(但隐藏)并且应该执行您想要的操作(但是,我不确定这适用于哪个 API 级别)——但由于某种原因不在表中.
/**
* Wake lock that turns the screen off when the proximity sensor activates.
* Since not all devices have proximity sensors, use
* {@link #getSupportedWakeLockFlags() getSupportedWakeLockFlags()} to determine if
* this wake lock mode is supported.
*
* {@hide}
*/
public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK = WAKE_BIT_PROXIMITY_SCREEN_OFF;
If you aren't afraid of using a potential undocumented feature, it should do exactly what you need.
如果您不害怕使用潜在的未记录功能,它应该完全满足您的需求。
回答by guness
as of API level 21 (Lollipop) you can get proximity wake lock this just like that:
从 API 级别 21 (Lollipop) 开始,您可以像这样获得接近唤醒锁定:
if(powerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) {
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TAG);
wakeLock.setReferenceCounted(false);
return wakeLock;
} else {
return null;
}
}
then it is up to you to acquire and release the lock.
然后由您来获取和释放锁。
PS: PowerManager#getSupportedWakeLockFlags
was hidden, but now exists nomore. They have invented isWakeLockLevelSupported
instead.
PS: PowerManager#getSupportedWakeLockFlags
被隐藏,但现在不存在了。他们发明了isWakeLockLevelSupported
。
回答by rac2030
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/
可能您不再需要它了,但是对于那些对代码感兴趣的人,您可以在http://code.google.com/p/speakerproximity/ 上查看我的 SpeakerProximity 项目