Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending AsyncTask<Void, Void, Void>

Tags:

java

android

In my android app I am performing some operations in the doInBackground by extending AsyncTask<Void, Void, Void> class. (I have no use in performing any UI in this class)

  1. Is this proper use of AsyncTask ?
  2. If so can I extend AsyncTask instead ?
  3. What is the difference between extending AsyncTask and AsyncTask<Void, Void, Void>

Code example:

public class MessagePooling extends AsyncTask<Void, Void, Void> {             @Override     protected Void doInBackground(Void... params)      {         while (!isCancelled())          {                       //Getting data from server                         SystemClock.sleep(1000);         }         return null;     } } 

Or:

public class MessagePooling extends AsyncTask {     @Override     protected Object doInBackground(Object... params)      {         while (!isCancelled())          {                       //Getting data from server                         SystemClock.sleep(1000);         }         return null;         } } 

Thanks

like image 251
Rami Avatar asked Jun 25 '12 17:06

Rami


People also ask

How do I extend AsyncTask?

Simple - you just use the Void class. So you would have private class DownloadFilesTask extends AsyncTask<Void, Integer, Long> { .

What can I use instead of AsyncTask?

Alternative 1: Using Executor and Handler The executor will help in performing any task in the background and the handler will help to make UI changes.

Is AsyncTask deprecated?

This class was deprecated in API level 30.

How many times an instance of AsyncTask can be executed?

AsyncTask instances can only be used one time.


2 Answers

The AsyncTask class can be thought of as a very convenient threading mechanism. It gives you a few tools that you can use that simple Java threads simply don't have such as on cancel cleanup operations. You don't have to do any UI in the background. You could simply execute one by writing one as an anonymous class like this:

    new AsyncTask<Integer, Void, Void>(){         @Override         protected Void doInBackground(Integer... params) {             // **Code**             return null;         }     }.execute(1, 2, 3, 4, 5); 

It will execute whatever you put in doInBackground on a background thread with the given parameters. Likewise, you can simply use Void and execute with no parameters.

The only advantage I could think of executing a thread this way would be to aid in future maintenance. There might be a case where you want to modify certain things that are required to be on the UI thread, in which case you would override the other methods. Other cases would be you simply don't do the action enough to justify writing out another class, so just create one on the fly and be done with it.

EDIT:

To answer #3: they're effectively the same. The Void object is a Java object just like anything else. You're not using Void, so what you use in it's place doesn't matter. It's just the AsyncTask contract requires three class types to be passed in, and by default they're Object which is the baseline class of everything.

like image 165
DeeV Avatar answered Sep 25 '22 20:09

DeeV


(I have no use in performing any UI in this class)

Then just use the normal Thread class rather than using AsyncTask class which is designed for dealing with UI changes during the thread's life time:

Runnable r = new Runnable() {     @Override     public void run()     {         // your code here     } };  Thread t = new Thread(r); t.start(); 
like image 31
Eng.Fouad Avatar answered Sep 25 '22 20:09

Eng.Fouad