屏幕关闭时接近传感器不起作用?
您好,
我有一个与接近传感器相关的问题。当我将手指放在上面时,我想关闭屏幕,当我拿开手指时,我想打开屏幕。 我成功地完成了关闭部分,但是当我将手指从传感器上移开时,它似乎没有执行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了问题的答案,并设法弄清楚了接近传感器的主要流程。
首先,有两种处理接近传感器的方法:
1. 对 PROXIMITY_SCREEN_OFF_WAKE_LOCK 进行锁定。从我读到的内容来看,这在某种程度上是android使用的一个隐藏常量,其值为32(至少目前是这样——在未来的版本中可能会或可能不会改变)。如果锁定成功,接近传感器将像您在通话中一样工作。
注意:
希望这对某人有帮助。
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.
Note:
Hope this is of help for somebody.
我猜你的程序是一个“在屏幕上”运行的活动?
我对这个主题了解不多,但根据我在 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.