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



我想在接近脸部时关闭屏幕。我用这个来关闭屏幕。
但是我有一个错误:不能找到符号在这行:

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

您需要添加这个导入:

import android.view.WindowManager.LayoutParams;

尝试添加LayoutParams.FLAG_KEEP_SCREEN_ON;部分的前缀。params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; .

相关内容

  • 没有找到相关文章

最新更新