Android在取消计时器后重新启动计时器

时间:2022-10-11 15:30:31

I have a timer in my application that launches an AsyncTask every 15 secs.

我的应用程序中有一个计时器,它每15秒启动一次AsyncTask。

Timer timer = new Timer();

public void AsynchTaskTimer() {
    final Handler handler = new Handler();

    TimerTask timertask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    try {
                        new updateGPSTask().execute();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                    }
                }
            });
        }
    };
    timer.schedule(timertask, 0, 15000); // execute in every 15sec
}

This is launched from the onCreate() method.

这是从onCreate()方法启动的。

When I call another activity I need to cancel this timer, which I did using timer.cancel() on my onPause() method in my Main Activity.

当我调用另一个活动时,我需要取消这个计时器,我在主活动中的onPause()方法上使用了timer.cancel()。

Now when I return to the Main Activity I need to restart the timer. I tried relaunching the AsynchTaskTimer() in the onRestart() method, but I get a java.lang.IllegalStateException: Timer was canceled.

现在,当我返回到主活动时,我需要重新启动计时器。我尝试在onRestart()方法中重新启动AsynchTaskTimer(),但是我得到了java.lang。IllegalStateException:定时器被取消了。

How do I restart my timer?

我如何重新启动我的计时器?

3 个解决方案

#1


14  

Try using :

尝试使用:

public void AsynchTaskTimer() {
    final Handler handler = new Handler();

    TimerTask timertask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    try {
                        new updateGPSTask().execute();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                    }
                }
            });
        }
    };
    timer = new Timer(); //This is new
    timer.schedule(timertask, 0, 15000); // execute in every 15sec
    }

By using this you can again allocate the memory to Timer and start it AFAIK.

通过使用它,您可以再次将内存分配给Timer并启动它。

#2


13  

So what says docs about cancel() method:

那么关于cancel()方法的文档是怎么说的呢?

Note that calling this method from within the run method of a repeating timer task absolutely guarantees that the timer task will not run again.

注意,从重复计时器任务的run方法中调用此方法绝对保证计时器任务不会再次运行。

It means that if you once cancel() Timer you can't to run it again. You need to create new instance of Timer, for example in onResume() method if you want to run it again.

这意味着一旦取消了()定时器,就不能再运行它。您需要创建Timer的新实例,例如在onResume()方法中,如果您想再次运行它。

@Override
public void onResume() {
   super.onResume();
   // intialise new instance of Timer
}

#3


1  

Instead of using TimerTask, you can move your code (which is used to update GPS) to a service. I think it's better. You can call startService() in onCreate() and stopService() in onDestroy(). Note that services run on main UI thread, so make sure to avoid of NetworkOnMainThreadException in your GPS updater task.

您可以将您的代码(用于更新GPS)移动到服务,而不是使用TimerTask。我认为这是更好的。可以在onCreate()中调用startService(),在onDestroy()中调用stopService()。注意,服务运行在主UI线程上,所以请确保在您的GPS更新任务中避免NetworkOnMainThreadException。

#1


14  

Try using :

尝试使用:

public void AsynchTaskTimer() {
    final Handler handler = new Handler();

    TimerTask timertask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    try {
                        new updateGPSTask().execute();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                    }
                }
            });
        }
    };
    timer = new Timer(); //This is new
    timer.schedule(timertask, 0, 15000); // execute in every 15sec
    }

By using this you can again allocate the memory to Timer and start it AFAIK.

通过使用它,您可以再次将内存分配给Timer并启动它。

#2


13  

So what says docs about cancel() method:

那么关于cancel()方法的文档是怎么说的呢?

Note that calling this method from within the run method of a repeating timer task absolutely guarantees that the timer task will not run again.

注意,从重复计时器任务的run方法中调用此方法绝对保证计时器任务不会再次运行。

It means that if you once cancel() Timer you can't to run it again. You need to create new instance of Timer, for example in onResume() method if you want to run it again.

这意味着一旦取消了()定时器,就不能再运行它。您需要创建Timer的新实例,例如在onResume()方法中,如果您想再次运行它。

@Override
public void onResume() {
   super.onResume();
   // intialise new instance of Timer
}

#3


1  

Instead of using TimerTask, you can move your code (which is used to update GPS) to a service. I think it's better. You can call startService() in onCreate() and stopService() in onDestroy(). Note that services run on main UI thread, so make sure to avoid of NetworkOnMainThreadException in your GPS updater task.

您可以将您的代码(用于更新GPS)移动到服务,而不是使用TimerTask。我认为这是更好的。可以在onCreate()中调用startService(),在onDestroy()中调用stopService()。注意,服务运行在主UI线程上,所以请确保在您的GPS更新任务中避免NetworkOnMainThreadException。