如何根据重要的光值使振动不同?

How to make the vibrate different according to the significant light value?

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

 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]..它会将值保存在全局变量中,您将在计算振动的地方使用它..