سؤال

I created a TimerTask with the following code:

    Screen_Timer_Task = new TimerTask() {

    @Override
    public void run() {

    runOnUiThread(new Runnable()
    {
            @Override
            public void run()
            {

                    // Do my stuff

            }
    });

    }

    };

Then, I start that TimerTask with that:

    Screen_Timer.scheduleAtFixedRate(Screen_Timer_Task, 1, 1);

This is working fine. But after calling

    Screen_Timer.cancel();

    Screen_Timer.purge();

it will not restart with

    Screen_Timer.scheduleAtFixedRate(Screen_Timer_Task, 1, 1);

The app is just crashing!!

هل كانت مفيدة؟

المحلول

public void start_my_timer()
{

    Screen_Timer_Task = new TimerTask() {

        @Override
        public void run() {


                runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                                // Do my stuff
                    }
                });

        }

    };

    Screen_Timer.schedule(Screen_Timer_Task, 1, 1);

}

نصائح أخرى

"The task may be run once " from docs timerTask

You must create TimerTask object again if you want to use it.

Also make Timer once again because cancel(): "Cancels the Timer and all scheduled tasks. If there is a currently running task it is not affected. No more tasks may be scheduled on this Timer. Subsequent calls do nothing."

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top