我试图读取每 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;
}
需要咨询