Turn screen on and off programatically in android

爷,独闯天下 提交于 2019-12-10 06:06:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!