Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

how to run the async task at specific time? (I want to run it every 2 mins)

I tried using post delayed but it's not working?

    tvData.postDelayed(new Runnable(){      @Override     public void run() {         readWebpage();      }}, 100); 

In the above code readwebpage is function which calls the async task for me..

Right now below is the method which I am using

   public void onCreate(Bundle savedInstanceState) {           readwebapage();     }     public void readWebpage() {     DownloadWebPageTask task = new DownloadWebPageTask();     task.execute("http://www.google.com");     }     private class DownloadWebPageTask extends AsyncTask<String, Void, String> {     @Override     protected String doInBackground(String... urls) {         String response1 = "";         response1=read();                     //read is my another function which does the real work             response1=read();          super.onPostExecute(response1);         return response1;     }         protected void onPostExecute(String result) {            try {                 Thread.sleep(100);             } catch (InterruptedException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }              TextView tvData = (TextView) findViewById(R.id.TextView01);             tvData.setText(result);          DownloadWebPageTask task = new DownloadWebPageTask();         task.execute(new String[] { "http://www.google.com" });      }      } 

This is what I my code is and it works perfectly fine but the big problem I drains my battery?

like image 526
Shan Avatar asked Jun 01 '11 20:06

Shan


People also ask

How do I run async tasks on Android?

To start an AsyncTask the following snippet must be present in the MainActivity class : MyTask myTask = new MyTask(); myTask. execute(); In the above snippet we've used a sample classname that extends AsyncTask and execute method is used to start the background thread.

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.

How many times an instance of AsyncTask can be executed?

AsyncTask instances can only be used one time.

How many threads are there in async task in Android?

In newer Android versions, 5 threads are create by default, and the ThreadPoolExecutor will attempt to run the AsyncTask s on these 5 threads. If you create more than 5 AsyncTask s, it may either queue them or create new threads (but only up to 128).


1 Answers

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 Handler mHandler = new Handler();  Runnable mHandlerTask = new Runnable() {      @Override       public void run() {           doSomething();           mHandler.postDelayed(mHandlerTask, INTERVAL);      } };  void startRepeatingTask() {     mHandlerTask.run();  }  void stopRepeatingTask() {     mHandler.removeCallbacks(mHandlerTask); } 

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.

like image 177
inazaruk Avatar answered Sep 27 '22 18:09

inazaruk