我建立了一个计时器,即使我从活动中退出,计时器仍然继续,当我离开当前活动时如何阻止它?
主题:
t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(5000);
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
output = new teste().execute(getconversationid).get();
setMyInt(output);
Log.e("", output);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
答案 0 :(得分:-1)
在onDestroy()或onStop()方法中,通过调用
来停止计时器线程t.kill();
另一方面,我建议使用一个简单的cound down计时器类,而不是自己构建一个。然后在onDestroy或onStop()方法中调用objectname.cancle()。
public class pauseTimer extends CountDownTimer {
public pauseTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
}
}