gpt4 book ai didi

android:靠近脸时关闭屏幕

转载 作者:可可西里 更新时间:2023-11-01 18:52:15 26 4
gpt4 key购买 nike

我的应用允许用户访问他们的公司语音邮件。通常,在通话过程中,当用户将设备放在耳边时,屏幕会关闭,这样他们就不会不小心用脸按下按钮。我想让我的应用程序在用户收听语音邮件时做同样的事情。

有人知道怎么做吗?

最佳答案

如果您被允许查看开源代码而不会给自己带来麻烦,请检查 Android Phone Application 的来源.具体src/com/android/phone/PhoneApp.javasrc/com/android/phone/InCallScreen.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);
}
}

此外,如果您查看 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;

如果您不害怕使用潜在的未记录的功能,它应该可以满足您的需求。

关于android:靠近脸时关闭屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3018716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com