5

I want to turn off screen when close to face. I've used this to turn screen off.
but I have an error : cannot find symbol in this line:

params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;

and

params.f  lags |= LayoutParams.FLAG_KEEP_SCREEN_ON;


Here is my code:

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);
   }
  }
}
}
Community
  • 1
  • 1
Farzan Najipour
  • 2,442
  • 7
  • 42
  • 81

2 Answers2

3

You need to add this import:

import android.view.WindowManager.LayoutParams;
antonio
  • 18,044
  • 4
  • 45
  • 61
1

Try to prefix the LayoutParams.FLAG_KEEP_SCREEN_ON; part. This might solve the problem: params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;.

JPS
  • 604
  • 5
  • 14