如何停止Timer线程? 我有一个计时器线程,我想阻止它。我该怎么做?
我试图通过停止线程停止但是没有成功。 有任何想法吗? 提前感谢您的帮助
这是我的代码:
public void startTimer() {
startTime = System.currentTimeMillis();
final Handler handler = new Handler();
task =new Thread() {
@Override
public void run() {
long millis = System.currentTimeMillis()-startTime;
long secs = millis / 1000 % 60; // seconds, 0 - 59
long mins = millis / 1000 / 60 % 60; // total seconds / 60, 0 - 59
long hours = millis / 1000 / 60 / 60; // total seconds / 3600, 0 - limitless
timeString = String.format("%02d:%02d:%02d", hours, mins, secs);
runOnUiThread(new Runnable() {
@Override
public void run() {
mtvtimer.setText(timeString);
}
});
handler.postDelayed(task, 1000);
}
};
task.start();
}
答案 0 :(得分:1)
鉴于不推荐使用Thread.stop,唯一的选择就是添加一些你自己的信号,比如你希望它停止时设置的布尔变量。
e.g。
boolean stop = false;
public void startTimer() {
startTime = System.currentTimeMillis();
final Handler handler = new Handler();
task =new Thread() {
@Override
public void run() {
if (stop) {
return;
}
long millis = System.currentTimeMillis()-startTime;
long secs = millis / 1000 % 60; // seconds, 0 - 59
long mins = millis / 1000 / 60 % 60; // total seconds / 60, 0 - 59
long hours = millis / 1000 / 60 / 60; // total seconds / 3600, 0 - limitless
timeString = String.format("%02d:%02d:%02d", hours, mins, secs);
runOnUiThread(new Runnable() {
@Override
public void run() {
mtvtimer.setText(timeString);
}
});
handler.postDelayed(task, 1000);
}
从其他地方,你设置stop = true
来阻止它。
(这是一个粗略的示例代码,只是为了得到这个想法)
答案 1 :(得分:0)
从你发布的代码片段来看,你想要实现的目标并不是那么清楚。您正在传递任务,在处理程序中是TimerJob,同时,您调用task.start(),这意味着,每次任务启动时,它都会将另一个任务附加到处理程序,只要使用UiThread。因此,如果我理解了你的逻辑,你可以做类似下面的例子:
//Handler should be created only once , and should have the specified methods
//in which he is needed to respond ,
//for complicated tasks , create a new Specific Handler
final Handler handler = new Handler();
//Defining Thread for example
Thread task;
public void startTimer() {
startTime = System.currentTimeMillis();
task =new Thread() {
@Override
public void run() {
long millis = System.currentTimeMillis()-startTime;
long secs = millis / 1000 % 60; // seconds, 0 - 59
long mins = millis / 1000 / 60 % 60; // total seconds / 60, 0 -59
long hours = millis / 1000 / 60 / 60; // total seconds / 3600, 0 - limitless
timeString = String.format("%02d:%02d:%02d", hours, mins, secs);
runOnUiThread(new Runnable() {
@Override
public void run() {
mtvtimer.setText(timeString);
}
//repeat the task
handler.postDelayed(task, 1000);
});
}
};
//No Reason To start an unmanaged thread
//task.start();
//instead passing it to the handler ,
//and still you can call interrupt on the task reference
handler.postDelayed(task, 1000);
}
//To call whenever you have to stop the task
public void stopHandlerTask()
{
//your logic here
handler.removeCallbacks(task);
}
答案 2 :(得分:0)
似乎这是计时器的工作:
//references for the timer, task and start time.
Timer timer;
TimerTask task;
long startTime;
public void onCreate()
{
task = new TimerTask() {
@Override
public void run() {
long millis = System.currentTimeMillis()-startTime;
long secs = millis / 1000 % 60; // seconds, 0 - 59
long mins = millis / 1000 / 60 % 60; // total seconds / 60, 0 -59
long hours = millis / 1000 / 60 / 60; // total seconds / 3600, 0 - limitless
mtvtimer.setText(String.format("%02d:%02d:%02d", hours, mins, secs));
}
};
}
//start the timer
public void startTimer()
{
timer = new Timer();
timer.schedule(task, 1000, 1000);// start after 1 second and repeat after every second.
}
//stop the timer
public void stopTimer()
{
if(timer != null) {
timer.cancel();
timer.purge();
timer = null;
}
}