How to turn on and off the display in android?

338 Views Asked by At

I want to disable the input(or turn off display) using proximity sensor.

The screen input must be disabled and enabled(turn off and turn on) by the proximity sensor change.

I have searched many questions here, but didn't find any suitable answer. In the code I have tried the display become off. But when the proximity changes the display didn't turn ON.

This is the onSensorChanged method:

public void onSensorChanged(SensorEvent event) {
     // TODO Auto-generated method stub
     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); 


          } 
    }
0

There are 0 best solutions below