与屏幕的工作距离感应器关闭? [英] Proximity sensor not working with screen turned off?
问题描述
海兰,
我有相关的非接触式传感器中的问题。当我把手指放在上面,我要关闭屏幕,当我乘坐的手指,我想打开屏幕。
我成功地做了关闭部分,但是当我把手指离开传感器,它似乎并没有执行onSensorChanged方法。这里是code吧:
公共无效onSensorChanged(SensorEvent事件){
浮距离= event.values [0];
布尔有效=(距离> = 0.0&放大器;&安培;距离< PROXIMITY_THRESHOLD&放大器;&安培;距离< event.sensor.getMaximumRange());
布尔isValidCallState = FALSE; 如果(callsInfo!= NULL){
对于(SipCallSession callInfo:callsInfo){
INT状态= callInfo.getCallState();
isValidCallState | =((状态== SipCallSession.CallState.CONFIRMED)
|| (状态== SipCallSession.CallState.CONNECTING)
|| (状态== SipCallSession.CallState.CALLING)|| (状态== SipCallSession.CallState.EARLY&放大器;&安培;!callInfo
.isIncoming()));
}
}
如果(isValidCallState&安培;&安培;有效){
Log.e(,关闭);
WindowManager.LayoutParams LP = getWindow()的getAttributes()。
lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF; 。getWindow()setAttributes(LP);
lockOverlay.show();
}其他{
Log.e(,打开);
WindowManager.LayoutParams LP = getWindow()的getAttributes()。 lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
。getWindow()setAttributes(LP);
}
Log.e(,转???); // - >释放传感器时不会打印此
}
任何想法?谢谢你。
我找到了答案,我的问题,并设法找出接近传感器的主要流程。
首先,有2种方式来处理接近传感器:
1.在PROXIMITY_SCREEN_OFF_WAKE_LOCK锁。从我读这是某种由Android使用一个隐藏的常量,具有值32(至少在时刻 - 可能会或可能不会在将来的版本中改变)。如果锁成功,接近传感器,将表现为你正处于通话状态。
{尝试
方法方法= powerManager.getClass()。getDeclaredMethod(
getSupportedWakeLockFlags);
INT supportedFlags =(整数)method.invoke(电源管理);
域F = PowerManager.class.getDeclaredField(PROXIMITY_SCREEN_OFF_WAKE_LOCK);
INT proximityScreenOffWakeLock =(整数)f.get(NULL);
如果((supportedFlags&安培;!proximityScreenOffWakeLock)=为0x0){
proximityWakeLock = powerManager.newWakeLock(proximityScreenOffWakeLock,
com.voalte.CallProximity);
proximityWakeLock.setReferenceCounted(假);
} }赶上(例外五){
}
- 注册为传感器的侦听器。在这种情况下,非接触式传感器的动作,必须在onSensorChanged方法(screenBrigthness可设置)来处理。
请注意:
- 当屏幕被关闭时,所述的onPause被调用。
- 如果u使用第一种方法,U应该考虑也有万一后备的PROXIMITY_SCREEN_OFF_WAKE_LOCK不能设置。因此,我建议尝试实例化在onStart锁和onResume检查它反对无效。如果不为null,AQUIRE锁,用于传感器,否则注册监听器。
- 在onSensorChanged我设置为screenBrightness BRIGHTNESS_OVERRIDE_OFF,覆盖传感器时和relesing当我设置为screenBrightness和BRIGHTNESS_OVERRIDE_FULL /或BRIGHTNESS_OVERRIDE_NONE,但它并没有打开屏幕。因此,一个解决方法是使被设置为不可见默认情况下,可见黑色布局,占据了整个屏幕。
希望这是对别人的帮助。
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.
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) {
}
- 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:
- When the screen is turned off, the onPause is called.
- 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.
- 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.
这篇关于与屏幕的工作距离感应器关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!