我有一個與接近傳感器有關的問題。當我將手指放在上面時,我想關閉屏幕,當我拿起手指時,我想打開屏幕。 我成功完成了關閉部分,但是當我將手指從傳感器上取下時,似乎並未執行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
}
任何想法?謝謝。
我試着做一些鎖。首先,我嘗試了:PARTIAL_WAKE_LOCK,然後PROXIMITY_SCREEN_OFF_WAKE_LOCK和32值,但沒有工作:( – Andrei 2012-01-31 09:20:10