I have implemented something like this and it works fine:
private Timer mTimer1;
private TimerTask mTt1;
private Handler mTimerHandler = new Handler();
private void stopTimer(){
if(mTimer1 != null){
mTimer1.cancel();
mTimer1.purge();
}
}
private void startTimer(){
mTimer1 = new Timer();
mTt1 = new TimerTask() {
public void run() {
mTimerHandler.post(new Runnable() {
public void run(){
//TODO
}
});
}
};
mTimer1.schedule(mTt1, 1, 5000);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…