[英]Can not stop TimerTask
I have a problem with my Java code when doing with Wii Remote. 使用Wii Remote时,我的Java代码存在问题。 My situation is "When button A presses, print something to the screen (the raw acceleration in my case) until releasing button A", so, after few days of searching, i use TimerTask and Timer for doing task.
我的情况是“当按钮A按下时,在屏幕上打印一些东西(在我的情况下是原始加速度)直到释放按钮A”,所以,经过几天的搜索,我使用TimerTask和Timer来完成任务。 It printed the raw acceleration.
它打印了原始加速度。 Howerver, after i release button A, it cannot stop printing.
Howerver,在我释放按钮A后,它无法停止打印。 Here is my code:
这是我的代码:
public class TestTimerTask implements WiimoteListener {
public RawAcceleration rawacc;
public static void main(String[] args){
Wiimote[] wiimotes = WiiUseApiManager.getWiimotes(1, true);
Wiimote wiimote = wiimotes[0];
wiimote.activateMotionSensing();
wiimote.toString();
wiimote.addWiiMoteEventListeners(new TestTimerTask ());
}
@Override
public void onButtonsEvent(WiimoteButtonsEvent wbe) {
boolean check = false;
Timer timer = new Timer();
TaskNeedToDo task = new TaskNeedToDo();
if(wbe.isButtonAJustReleased()){
System.out.println("Button A Just Released");
task.setCheck(false);
task.cancel();
timer.cancel();
}
if(wbe.isButtonAJustPressed()){
// check = false;
try {
System.out.println("Button A Just Pressed");
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:sss");
Date temp = new Date (System.currentTimeMillis());
Date now = dateFormatter.parse(dateFormatter.format(temp));
int period = 300;
task.setCheck(true);
task.setRawAcc(rawacc);
timer.schedule(task, 0, period);
} catch (ParseException ex) {
System.out.println("Error when take current time");
}
}
if(wbe.isButtonBHeld())
WiiUseApiManager.shutdown();
}
@Override
public void onIrEvent(IREvent ire) {
System.out.println("onIrEvent oocur");
}
@Override
public void onMotionSensingEvent(MotionSensingEvent mse) {
rawacc = mse.getRawAcceleration();
}
class TaskNeedToDo extends TimerTask {
private boolean check;
private RawAcceleration value;
public boolean getCheck(){
return check;
}
public void setCheck(boolean check){
this.check = check;
if(check == false)
this.cancel();
}
public RawAcceleration getRawAcc(){
return value;
}
public void setRawAcc(RawAcceleration value){
this.value = value;
}
@Override
public void run() {
if(check == false)
this.cancel();
System.out.println(value);
System.out.println(check);
}
}
And here is the result: 这是结果:
Button A Just Pressed
Raw acceleration : (14, 115,148)
true
Raw acceleration : (14, 115,148)
true
Raw acceleration : (14, 115,148)
true
Button A Just Released
Raw acceleration : (14, 115,148)
true
Raw acceleration : (14, 115,148)
true
Raw acceleration : (14, 115,148)
true
Raw acceleration : (14, 115,148)
true
Raw acceleration : (14, 115,148
true
The check stills true, so it cannot stop :( Thanks for helping me :D 支票仍然是真的,所以它不能停止:(谢谢你帮助我:D
When you are calling timer.cancel()
you are calling it on a new instance of timer. 当你调用
timer.cancel()
你在一个新的计时器实例上调用它。 So you are not cancelling the timer that you had previously scheduled. 因此,您不会取消之前安排的计时器。
I would suggest storing the timer as a class level field. 我建议将计时器存储为类级字段。 Make sure it is only instantiated once (with
new Timer()
) and then ensure that when you call cancel it is the same instance that you called schedule on. 确保它只实例化一次(使用
new Timer()
),然后确保在调用cancel时它与您调用schedule的实例相同。
Uber simplified version: 优步简化版:
public class TestTimerTask implements WiimoteListener {
private Timer timer = new Timer();
@Override
public void onButtonsEvent(WiimoteButtonsEvent wbe) {
if(wbe.isButtonAJustReleased()) {
timer.cancel();
}
if(wbe.isButtonAJustPressed()) {
timer.schedule(...);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.