Turning the screen on and off in Android software

I want to turn on and off the screen based on the proximity sensor. I can turn off the screen. but the code on the screen does not work back. Can anybody help me? 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);
      } 
}`
+4
source share
2 answers

First, I reduced the screen brightness as low as possible, and then made all GUI elements inaccessible to touch problems. Below 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 turned to turn off the touch screen to view the entire screen.

+1
source

To turn on the screen brightness up to on, the value is 1 and to turn it on the offvalue is 0.

0

All Articles