1

In my code I have a thread. You can see the code of thread,

public class MainAsyncHome extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {

            return null;
        }

        @Override
        protected void onPostExecute(String xml) {


        }

        @Override
        protected void onPreExecute() {


        }

        @Override
        protected void onProgressUpdate(Void... values) {

        }


    }

I run this thread in my main activity onCreate method as following way

new MainAsyncHome().execute(null);

But I want to give time out for this thread. It means when main activity run I want late to run this thread. I know it can do using sleep method. But How I can late for running this thread just like that way. I'm stuck with this problem. pls give me answer. Thanks

madu243
  • 243
  • 2
  • 7
  • 18
  • possible duplicate of [How to run an async task for every x mins in android?](http://stackoverflow.com/questions/6207362/how-to-run-an-async-task-for-every-x-mins-in-android) – g00dy Jul 04 '13 at 12:39
  • how timeout is connected with delayed start ... – Selvin Jul 04 '13 at 12:40

3 Answers3

4

Use Handler class, and define Runnable handleMyAsyncTask that will contain code executed after 3000 msec delay:

mHandler.postDelayed(MainAsyncHome, 1000*3); //Delay of three seconds

The answer is taken from here.

To put it in the code:

private final static int INTERVAL = 1000 * 3; //3 seconds
Handler m_handler;

Runnable MainAsyncHome = new Runnable()
{
     @Override 
     public void run() {
          doSomething();
          m_handler.postDelayed(MainAsyncHome, INTERVAL);
     }
}

void startRepeatingTask()
{
    MainAsyncHome.run(); 
}

void stopRepeatingTask()
{
    mHandler.removeCallback(MainAsyncHome);
}

Hope it works.

Community
  • 1
  • 1
g00dy
  • 6,752
  • 2
  • 30
  • 43
  • People should learn about the Handler class. People should take the time to understand how to use and program the system before going ahead and dumping SO with misaddressed questions. Thanks for promoting the Handler it is absolutely vital for Android development! – allprog Jul 04 '13 at 12:46
  • got error message "The method removeCallback(Runnable) is undefined for the type Runnable". I want to create handler class sepereatly, Can't I put this code in mainActivity – madu243 Jul 04 '13 at 12:48
  • Try replacing `mHandler.removeCallback(MainAsyncHome);` with `m_handler.removeCallbacksAndMessages(null);`. If this works ok, I'll edit my reply. – g00dy Jul 04 '13 at 13:27
0

I normally use CountDownTimer, suppose a 3 seconds of delay:

CountDownTimer timer = new CountDownTimer(3000, 1000) {

     public void onTick(long millisUntilFinished) {

     }

     public void onFinish() {
         //do things, start your Task
         //remember we are still in the main thread!
     }
  }.start();

Get more info at: http://developer.android.com/reference/android/os/CountDownTimer.html

Seraphim's
  • 12,559
  • 20
  • 88
  • 129
0

Use a CountDownTimer like this.

Start your timer in onCreate.

CountDownTimer timer=new CountDownTimer(Delay,TimeIntervalToCallOnTick) {

            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                //start your asynctask
            }
        };
ridoy
  • 6,274
  • 2
  • 29
  • 60
kaushal trivedi
  • 3,405
  • 3
  • 29
  • 47