0

I built one AsyncTask class that returns a specific value. This value changes frequently, so, I need to call my AsyncTask class multiple times to show the value updated.

I'm getting on a different class the result from the AsyncTask.

     try {
        output = new task().execute(getconversationid).get();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

And when the result from the AsyncTask updates, I call my other Class again to update everything.

     private void call() {

     new GetContacts().execute(id);

     }

    ...

    mobilemessages = contacts.length();

    ...

     myNum = Integer.parseInt(output);

     if(myNum != mobilemessages) {
         call();
     }

My question is how can i set a Timer or a Handler to update my class call(task) every three seconds?

Thank you.

Michael Jackson
  • 356
  • 3
  • 18
  • 2
    I wouldn't handle it this way. Why don't you just create a background thread that updates your value and then send it to a handler to update your UI when needed? – zgc7009 May 01 '15 at 14:48
  • i didn't think about that, its so much easier, thanks – Michael Jackson May 01 '15 at 14:50
  • Here you go: http://stackoverflow.com/questions/2258066/java-run-a-function-after-a-specific-number-of-seconds – ScarletMerlin May 01 '15 at 14:51
  • I would use `Observable.interval().map(i -> yourCall()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())subscribe(result -> doWhatYouHaveToDoWithIt());` (no asynctask at all) – njzk2 May 01 '15 at 15:01

1 Answers1

0

Use Thread.sleep(3000); 3000 is in milliseconds.

Try{

Thread.sleep(3000);

}catch(Exception ex){
}
Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24
  • unless combined with `publishProgress` (but then it would be recommended to run your asynctask on a dedicated executor, to avoid blocking all other asynctasks) – njzk2 May 01 '15 at 14:58