在android-turn屏幕关闭时使用接近传感器

时间:2015-05-18 07:34:40

标签: java android proximity

我想在靠近脸时关掉屏幕。我已使用this关闭屏幕。
但我有一个错误: 在此行中找不到符号
< / p>

params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;

params.f  lags |= LayoutParams.FLAG_KEEP_SCREEN_ON;

这是我的代码:

import org.qtproject.qt5.android.bindings.QtActivity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.Toast;
import android.os.PowerManager;
import android.view.WindowManager;

public class ProximitySensor extends Activity implements SensorEventListener{
 //SensorManager lets you access the device's sensors
 //declare Variables
 private SensorManager sensorManager;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  //create instance of sensor manager and get system service to interact with Sensor
  sensorManager= (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  WindowManager.LayoutParams params = getWindow().getAttributes();
  Sensor proximitySensor= sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
  .
  .
  .

 // called when sensor value have changed
 @Override
 public void onSensorChanged(SensorEvent event) {
  WindowManager.LayoutParams params = this.getWindow().getAttributes();
  if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){

      if(event.values[0]==0){
        params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
        params.screenBrightness = 0;
        getWindow().setAttributes(params);
   }
   else{
      params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
      params.screenBrightness = -1f;
      getWindow().setAttributes(params);
   }
  }
}
}

2 个答案:

答案 0 :(得分:1)

您需要添加此导入:

{{1}}

答案 1 :(得分:0)

尝试在LayoutParams.FLAG_KEEP_SCREEN_ON;部分前加上前缀。这可能会解决问题:params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;