我试着每隔x秒读取一次onsnsorchanged,我使用countdowntimer但不是每一秒都只有一次方法运行时,
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;
}
需要建议
答案 0 :(得分: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(...)
对象