[英]Turn screen on and off programatically in android
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);
}
}`
First I dimmed screen brightness as low as possible and then made all GUI elements unclickable for touch issues. 首先,我将屏幕亮度调暗尽可能低,然后使所有GUI元素都无法触及触摸问题。 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. 从这里开始 ,我参考了禁用触摸整个屏幕的视图。
要将屏幕亮度设置为on
,值为1,要将其off
则值为0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.