问题
I want to turn screen ON and OFF based on the proximity sensor. I am able to turn the screen off. but the code to ON the screen back is not working. Can anyone help me please? This is the code:`
public void onSensorChanged(SensorEvent event) {
if (event.values[0] == 0) {
Toast.makeText(getApplicationContext(), "sensor in 0",Toast.LENGTH_LONG).show();
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);
} else {
Toast.makeText(getApplicationContext(), "sensor in 1",Toast.LENGTH_LONG).show();
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = -1;
getWindow().setAttributes(params);
}
}`
回答1:
First I dimmed screen brightness as low as possible and then made all GUI elements unclickable for touch issues. Following is my code:
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
WindowManager.LayoutParams params = this.getWindow().getAttributes();
if (event.values[0] == 0) {
//TODO Store original brightness value
params.screenBrightness = 0.005f;
this.getWindow().setAttributes(params);
enableDisableViewGroup((ViewGroup)findViewById(R.id.YOUR_MAIN_LAYOUT).getParent(),false);
Log.e("onSensorChanged","NEAR");
} else {
//TODO Store original brightness value
params.screenBrightness = -1.0f;
this.getWindow().setAttributes(params);
enableDisableViewGroup((ViewGroup)findViewById(R.id.YOUR_MAIN_LAYOUT).getParent(),true);
Log.e("onSensorChanged","FAR");
}
}
From here, I took reference to disable touch for entire screen's view.
回答2:
To turn screen brightness to on
the value is 1 and to turn it off
the value is 0.
来源:https://stackoverflow.com/questions/21112954/turn-screen-on-and-off-programatically-in-android