4

我想根据接近传感器打开和关闭屏幕。我可以关闭屏幕。但是屏幕上的代码不起作用。任何人都可以帮助我吗?这是代码:`

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

2 回答 2

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");  
    }       
}  

从这里,我参考了禁用整个屏幕视图的触摸。

于 2014-06-23T02:49:17.140 回答
0

将屏幕亮度设置on为 1,将其off设置为 0。

于 2014-02-20T15:45:54.210 回答