How to stop the Timer thread?

2019-08-21 16:49发布

How to stop the Timer thread? I have a timer thread and I would like to stop it. How do I do it?

I tried to stop by stopping the thread but it was unsuccessful. Any ideas? Thank you in advance for your help

this is my code:

   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();


    }

标签: android timer
3条回答
做自己的国王
2楼-- · 2019-08-21 17:30

Given that Thread.stop is deprecated, the only option you have is to add some signalling of your own, like a boolean variable that you set when you want it to 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);
    }

And from somewhere else, you set stop = true to stop it. (this is a rough example code, just to get the idea through)

查看更多
够拽才男人
3楼-- · 2019-08-21 17:30

well from the snippet you posted , it is not so clear what you want to achieve. You are passing the task , in the handler to be TimerJob and at the same time , you call task.start() , which means , every time the task is starting , it will attach another task to the handler , as long as with the UiThread. So if i have understood correctly your logic , you could do something like the below example :

 //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);
}
查看更多
太酷不给撩
4楼-- · 2019-08-21 17:38

Seems like this is a job for a timer:

//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;
    }
}
查看更多
登录 后发表回答