0

我试图读取每 x 秒更改一次的传感器,我使用倒数计时器但不是每秒钟只有一次方法运行时,

private CountDownTimer delay;

@Override
public void onSensorChanged(SensorEvent event) {
    if (board!=null && event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        float[] values = event.values;
        // Movement
        x =(int) Math.floor(values[0]);
        y =(int) Math.floor(values[1]);
        z =(int) Math.floor(values[2]);
        delay = new countleft(START_DELAY, INTERVAL);
        delay.start();
    }
}


class countleft extends CountDownTimer{
    public countleft(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
    }
    @Override
    public void onFinish() {
        if(x>=3){
            board.right();
        }
        else if(x<=(-3)){
            board.left();
        }
        else if(y>=3){
            board.up();
        }
        else if(y<=(-3)){               
            board.down();
        }
        START_DELAY = 2000;
    }

需要咨询

4

1 回答 1

0

我建议onSensorChanged(...)你改变

if (board!=null && event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)

if (delay != null && board!=null && event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)

这样在任何时间点都只有一个延迟对象。然后,在课堂countleft.onFinish()上,而不是

START_DELAY = 2000;

你需要

delay = null;

以便countleft可以创建下一个对象onSensorChanged(...)

于 2013-05-30T22:33:11.407 回答