Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute Async task repeatedly after fixed time intervals

How to make Async task execute repeatedly after some time interval just like Timer...Actually I am developing an application that will download automatically all the latest unread greeting from the server and for that purpose I have to check for updates from server after some fixed time intervals....I know that can be easily done through timer but I want to use async task which I think is more efficient for android applications.

like image 787
Waseem Avatar asked Jun 30 '11 08:06

Waseem


People also ask

How many times an instance of async task can be executed?

AsyncTask instances can only be used one time.

Can we run multiple async task at a time?

There is a limit of how many tasks can be run simultaneously. Since AsyncTask uses a thread pool executor with limited max number of worker threads (128) and the delayed tasks queue has fixed size 10, if you try to execute more than 138 your custom tasks the app will crash with java.

Is TimerTask asynchronous?

Using TimerTask() for activating async task via handler is a bad idea. on orientation change you must remember to cancel also the timer and the handler calls. if not it will call the async task again and again on each rotation.

Why is async task being deprecated?

This class was deprecated in API level 30. AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.


2 Answers

public void callAsynchronousTask() {     final Handler handler = new Handler();     Timer timer = new Timer();     TimerTask doAsynchronousTask = new TimerTask() {                @Override         public void run() {             handler.post(new Runnable() {                 public void run() {                            try {                         PerformBackgroundTask performBackgroundTask = new PerformBackgroundTask();                         // PerformBackgroundTask this class is the class that extends AsynchTask                          performBackgroundTask.execute();                     } catch (Exception e) {                         // TODO Auto-generated catch block                     }                 }             });         }     };     timer.schedule(doAsynchronousTask, 0, 50000); //execute in every 50000 ms } 
like image 91
Rasel Avatar answered Sep 27 '22 21:09

Rasel


  //Every 10000 ms           private void doSomethingRepeatedly() {       Timer timer = new Timer();       timer.scheduleAtFixedRate( new TimerTask() {             public void run() {                    try{                       new SendToServer().execute();                     }                   catch (Exception e) {                       // TODO: handle exception                   }               }             }, 0, 10000);                      } 
like image 45
ritesh4326 Avatar answered Sep 27 '22 23:09

ritesh4326