How to run an async task afor every x mins in android?

You can use handler if you want to initiate something every X seconds. Handler is good because you don't need extra thread to keep tracking when firing the event. Here is a short snippet: private final static int INTERVAL = 1000 * 60 * 2; //2 minutes Handle m_handler; Runnable m_handlerTask = new Runnable() { @Override public void run() { doSomething(); m_handler.

PostDelayed(m_handlerTask, INTERVAL); } } void startRepeatingTask() { m_handlerTask.run(); } void stopRepeatingTask() { m_handler. RemoveCallback(m_handlerTask); } Note that doSomething should not take long (something like update position of audio playback in UI). If it can potentially take some time (like downloading or uploading to the web), then you should use ScheduledExecutorService s scheduleWithFixedDelay function instead.

You can use handler if you want to initiate something every X seconds. Handler is good because you don't need extra thread to keep tracking when firing the event. Here is a short snippet: private final static int INTERVAL = 1000 * 60 * 2; //2 minutes Handle m_handler; Runnable m_handlerTask = new Runnable() { @Override public void run() { doSomething(); m_handler.

PostDelayed(m_handlerTask, INTERVAL); } } void startRepeatingTask() { m_handlerTask.run(); } void stopRepeatingTask() { m_handler. RemoveCallback(m_handlerTask); } Note that doSomething should not take long (something like update position of audio playback in UI). If it can potentially take some time (like downloading or uploading to the web), then you should use ScheduledExecutorService's scheduleWithFixedDelay function instead.

I have edited your code remember 2000ms = 2 seconds; 120000ms = 2 minutes. – Jorgesys Jun 1 at 21:29 I actually fixed that before you submitted the change :) – inazaruk Jun 1 at 21:38 Whether it is m_handlerTask.run()? – Shan Jun 1 at 21:50 I didn't understand your question.

Could you elaborate? – inazaruk Jun 1 at 21:55 void startRepeatingTask() { m_handleTask.run(); } Whether this is right? When I post the code in eclipse it's showing an error?

This code should be in onCreate right? – Shan Jun 1 at 22:07.

Use Handler and PostDelayed new Handler(). PostDelayed(new Runnable(){ public void run(){ readWebpage(); } },120000);//Every 120000 ms (2 minutes).

Note that postDelayed() is also a method on View, so if you have a widget lying around, you do not need a Handler for this. – CommonsWare Jun 1 at 20:56 Thanks Mark good spot! – Jorgesys Jun 1 at 21:01 This does not run every 2 minutes.It runs once after 2 minute delay.

– inazaruk Jun 1 at 21:09 This code works but it's getting updated only once.. – Shan Jun 1 at 21:57.

Try extending the Thread class, set a sleep time of 2000 millis and place your call into the run method. That should do it.

You could run a loop within the AsyncTask that sleeps for two seconds between doing the tasks. Something like this: protected Result doInBackground (Params... params) { while (!interrupted) { doWork(); Thread. Sleep(2000); } }.

2 Running doInBackground() in an infinite loop is really a bad idea. Ideally using AsyncTask should be a one-shot operation and shouldn't run for an extended time. – MisterSquonk Jun 1 at 20:56 @MisterSquonk Fair call.

– Gary Buyn Jun 1 at 21:09.

You can use handler if you want to initiate something every X seconds. Handler is good because you don't need extra thread to keep tracking when firing the event. Note that doSomething should not take long (something like update position of audio playback in UI).

If it can potentially take some time (like downloading or uploading to the web), then you should use ScheduledExecutorService's scheduleWithFixedDelay function instead.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions