[英]Use Proximity Sensor in android- turn screen off
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);
}
}
}
}
您需要添加此导入:
import android.view.WindowManager.LayoutParams;
Try to prefix the LayoutParams.FLAG_KEEP_SCREEN_ON;
尝试为LayoutParams.FLAG_KEEP_SCREEN_ON;
添加前缀LayoutParams.FLAG_KEEP_SCREEN_ON;
part. 部分。 This might solve the problem: params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
这可能解决了这个问题: params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.