屏幕关闭时接近传感器不起作用?

发布于 2025-01-01 12:10:22 字数 1809 浏览 1 评论 0原文

您好,

我有一个与接近传感器相关的问题。当我将手指放在上面时,我想关闭屏幕,当我拿开手指时,我想打开屏幕。 我成功地完成了关闭部分,但是当我将手指从传感器上移开时,它似乎没有执行 onSensorChanged 方法。这是它的代码:

public void onSensorChanged(SensorEvent event) {
        float distance = event.values[0];
        boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD && distance < event.sensor.getMaximumRange());
        boolean isValidCallState = false;

        if (callsInfo != null) {
            for (SipCallSession callInfo : callsInfo) {
                int state = callInfo.getCallState();
                isValidCallState |= ((state == SipCallSession.CallState.CONFIRMED)
                        || (state == SipCallSession.CallState.CONNECTING)
                        || (state == SipCallSession.CallState.CALLING) || (state == SipCallSession.CallState.EARLY && !callInfo
                        .isIncoming()));
            }
        }
        if (isValidCallState && active) {
            Log.e("", "turn off");
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
            lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;

            getWindow().setAttributes(lp);
            lockOverlay.show();
        } else {
            Log.e("", "turn on");
            WindowManager.LayoutParams lp = getWindow().getAttributes();

            lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
            lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
            getWindow().setAttributes(lp);
        }
        Log.e("", "turn ???"); // --> does not print this when releasing the sensor
    }

有什么想法吗?谢谢。

Hy,

I have a problem related to the proximity sensor. When I put the finger on it, I want to turn the screen off and when I take the finger, I want to turn the screen on.
I successfully did the turning off part, but when I take the finger off the sensor, it does not seem to execute the onSensorChanged method. Here is the code for it:

public void onSensorChanged(SensorEvent event) {
        float distance = event.values[0];
        boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD && distance < event.sensor.getMaximumRange());
        boolean isValidCallState = false;

        if (callsInfo != null) {
            for (SipCallSession callInfo : callsInfo) {
                int state = callInfo.getCallState();
                isValidCallState |= ((state == SipCallSession.CallState.CONFIRMED)
                        || (state == SipCallSession.CallState.CONNECTING)
                        || (state == SipCallSession.CallState.CALLING) || (state == SipCallSession.CallState.EARLY && !callInfo
                        .isIncoming()));
            }
        }
        if (isValidCallState && active) {
            Log.e("", "turn off");
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
            lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;

            getWindow().setAttributes(lp);
            lockOverlay.show();
        } else {
            Log.e("", "turn on");
            WindowManager.LayoutParams lp = getWindow().getAttributes();

            lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
            lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
            getWindow().setAttributes(lp);
        }
        Log.e("", "turn ???"); // --> does not print this when releasing the sensor
    }

Any ideas? Thanks.

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

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

发布评论

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

评论(2

笑红尘 2025-01-08 12:10:22

我找到了问题的答案,并设法弄清楚了接近传感器的主要流程。

首先,有两种处理接近传感器的方法:
1. 对 PROXIMITY_SCREEN_OFF_WAKE_LOCK 进行锁定。从我读到的内容来看,这在某种程度上是android使用的一个隐藏常量,其值为32(至少目前是这样——在未来的版本中可能会或可能不会改变)。如果锁定成功,接近传感器将像您在通话中一样工作。

try {
                    Method method = powerManager.getClass().getDeclaredMethod(
                            "getSupportedWakeLockFlags");
                    int supportedFlags = (Integer) method.invoke(powerManager);
                    Field f = PowerManager.class.getDeclaredField("PROXIMITY_SCREEN_OFF_WAKE_LOCK");
                    int proximityScreenOffWakeLock = (Integer) f.get(null);
                    if ((supportedFlags & proximityScreenOffWakeLock) != 0x0) {
                        proximityWakeLock = powerManager.newWakeLock(proximityScreenOffWakeLock,
                                "com.voalte.CallProximity");
                        proximityWakeLock.setReferenceCounted(false);
                    }

                } catch (Exception e) {
                }
  1. 为传感器注册一个侦听器。在这种情况下,接近传感器操作必须在 onSensorChanged 方法中处理(可以设置屏幕亮度)。

注意:

  1. 当屏幕关闭时,onPause 会被调用。
  2. 如果您使用第一种方法,您应该考虑也有一个备份,以防无法设置 PROXIMITY_SCREEN_OFF_WAKE_LOCK。因此,我建议尝试在 onStart 和 onResume 中实例化锁,以检查是否为空。如果不为空,则获取锁,否则为传感器注册侦听器。
  3. 在 onSensorChanged 中,我将屏幕亮度设置为 BRIGHTNESS_OVERRIDE_OFF,当覆盖传感器和释放时,我将屏幕亮度设置为 BRIGHTNESS_OVERRIDE_FULL 和/或 BRIGHTNESS_OVERRIDE_NONE,但它没有打开屏幕。因此,解决方法是使默认设置为不可见的黑色布局变得可见并占据整个屏幕。

希望这对某人有帮助。

I found the answer to my question and managed to figure out the main flow of the proximity sensor.

First, there are 2 ways to handle the proximity sensor:
1. Make a lock on PROXIMITY_SCREEN_OFF_WAKE_LOCK. From what I read this is somehow a hidden constant used by android, that has the value 32(at least for the moment - might or might not be changed in future versions). If this lock succeeds, the proximity sensor, will behave as you are in a call.

try {
                    Method method = powerManager.getClass().getDeclaredMethod(
                            "getSupportedWakeLockFlags");
                    int supportedFlags = (Integer) method.invoke(powerManager);
                    Field f = PowerManager.class.getDeclaredField("PROXIMITY_SCREEN_OFF_WAKE_LOCK");
                    int proximityScreenOffWakeLock = (Integer) f.get(null);
                    if ((supportedFlags & proximityScreenOffWakeLock) != 0x0) {
                        proximityWakeLock = powerManager.newWakeLock(proximityScreenOffWakeLock,
                                "com.voalte.CallProximity");
                        proximityWakeLock.setReferenceCounted(false);
                    }

                } catch (Exception e) {
                }
  1. Register a listener for the Sensor. In this case the proximity sensor actions must be handled in the onSensorChanged method(screenBrigthness can be set).

Note:

  1. When the screen is turned off, the onPause is called.
  2. If u use the first method, u should consider to have also a back-up in case the PROXIMITY_SCREEN_OFF_WAKE_LOCK cannot be set. So I recommend to try and instantiate the lock in onStart and in onResume to check it against null. If not null, aquire the lock, otherwise register listener for the sensor.
  3. In the onSensorChanged I set the screenBrightness to BRIGHTNESS_OVERRIDE_OFF, when covering the sensor and when relesing I set the screenBrightness to BRIGHTNESS_OVERRIDE_FULL and/or BRIGHTNESS_OVERRIDE_NONE, but it didn't turn the screen on. So a workaround was to make a black layout that is set invisible by default, to be visible and occupy the full screen.

Hope this is of help for somebody.

撕心裂肺的伤痛 2025-01-08 12:10:22

我猜你的程序是一个“在屏幕上”运行的活动?

我对这个主题了解不多,但根据我在 android 上读过的一些文字,我认为 android 会在屏幕关闭时暂停/关闭应用程序。

您要么必须阻止这种暂停,要么在后台服务中运行您的应用程序。但我不知道服务是否可以以同样的方式读取传感器。

我不确定这个“答案”是否真的对您有帮助,但您可能会知道要搜索什么。

I guess that your program is an Activity that runs "on the screen"?

I do not know much about this subject, but based on some text I have read on android, I think android pauses/closes apps when the screen goes down.

Either you would have to prevent this pausing, or run your app in a background service. But I dont know if services can read the sensora in the same way.

Im not sure if this "answer" will really help you, but you might get some idea what to search for.

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