我想根据接近传感器打开和关闭屏幕。我可以关闭屏幕。但屏幕背面的代码无效。有人可以帮我吗? 这是代码:`
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);
}
}`
答案 0 :(得分:1)
首先,我将屏幕亮度调暗尽可能低,然后使所有GUI元素都无法触摸以解决触摸问题。以下是我的代码:
@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,我参考了禁用整个屏幕视图的触摸。
答案 1 :(得分:0)
要将屏幕亮度设置为on
,值为1并将其设为off
,则值为0.