gpt4 book ai didi

java - 如何根据显着光值使振动不同?

转载 作者:行者123 更新时间:2023-12-01 16:50:45 25 4
gpt4 key购买 nike

我希望应用程序根据检测到的光值相应地振动。例如,该值越高则振动越强。但是这段代码似乎不起作用,任何人都可以帮忙看看我的错误在哪里?能正确检测到光值,并且振动正在工作,但振动不根据光值振动。

 public class LightDetection extends AppCompatActivity {
TextView textLight, textOn, textOff;
Button btnStart, btnStop;
float x;
long ambientValue = 16;
long floor_delay = 500;
long ceiling_delay = 80;
long vibrate = 100;
long vibrate_delay;
Vibrator sensorVibrator;
SensorManager sensorManager;
Sensor sensor;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_light_detection);

sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
textLight = (TextView) findViewById(R.id.textView);
textOn = (TextView) findViewById(R.id.textView2);
textOff = (TextView) findViewById(R.id.textView3);
btnStart = (Button) findViewById(R.id.button3);
btnStop = (Button) findViewById(R.id.button4);


final SensorEventListener lightListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int acc) {
}

@SuppressLint("SetTextI18n")
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
Log.i("IE LightDetect.", "Light value: " + x);
textLight.setText((int) x + " lux");
}
};

btnStart.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetTextI18n")
public void onClick(View view) {
sensorManager.registerListener(lightListener, sensor,
SensorManager.SENSOR_DELAY_FASTEST);
handler.postDelayed(runnable, vibrate_delay);
textOn.setText("LIGHT DETECTION MODE: ON");
}
});

btnStop.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetTextI18n")
public void onClick(View view) {
textOn.setText("LIGHT DETECTION MODE: OFF");
sensorManager.unregisterListener(lightListener);
sensorVibrator.cancel();
handler.removeCallbacks(runnable);

}
});



}

private Runnable runnable = new Runnable() {
@Override
public void run() {
/* do what you need to do */
long luxValue = (long) x;
//Correction Ratio - Ceiling Delay/Most Likely Max Lux
long ratio = 950 / 300;

if (luxValue < ambientValue) {
vibrate = 100;
vibrate_delay = floor_delay + (ratio * luxValue);
} else {
vibrate = 100;
vibrate_delay = floor_delay - (ratio * luxValue);
}

if (vibrate_delay < ceiling_delay) {
long[] pattern = {0, vibrate, ceiling_delay};
sensorVibrator.vibrate(pattern, 1);
} else {
long[] pattern = {0, vibrate, vibrate_delay};
sensorVibrator.vibrate(pattern, 1);
}

Log.i("IE LightDetect.", "Lux: " + luxValue);
handler.postDelayed(this, vibrate_delay);
}
};

}`

最佳答案

onSensorChanged() 中获取光值后,您没有全局保存该值,您使用 float x = event.values[0] 这只是创建函数内的局部变量,而不是全局变量。尝试从那里放弃 float ,使用 x = event.values[0] 而不是 float x = event.values[0]..它将将该值保存在全局变量中,并且您将在计算振动时使用该值..

关于java - 如何根据显着光值使振动不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61695352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com