在我的項目中,我需要使用接近傳感器來管理屏幕燈。如何使用接近傳感器來管理屏幕燈:Android
從過去3天起,我試圖做同樣的事情。但我仍然沒有取得成功。
我的一半代碼工作正常。能夠使用接近傳感器關閉屏幕燈。但屏幕燈不亮。當我用手覆蓋傳感器時,屏幕指示燈熄滅。但從傳感器上取下手後,光線不會變亮。
我的代碼是:
@Override
public void onSensorChanged(SensorEvent event)
{
if(event.sensor.getType() == Sensor.TYPE_PROXIMITY)
{
switch (lastSensorPosition)
{
//case 1 will turn on screen light
case 1:
lastSensorPosition = 2;
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL;
getWindow().setAttributes(lp);
break;
//case 2 will turn off screen light
case 2:
lastSensorPosition = 1;
WindowManager.LayoutParams lp1 = getWindow().getAttributes();
lp1.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
getWindow().setAttributes(lp1);
break;
default:
break;
}
}
}
我在等你們有很大的幫助......