Asynctask of non ui thread

Do we need to initiate and execute asyctask from the user interface thread. Is it correct if I use asynctask to access the web service (long) from a non ui stream. Sorry if my request is incorrect.

In my application, I need to run about 10 web services and show the result on ui. I was confused about which approach would be a good asynthesis, intenservice, or creating a thread for each webservice call and ensuring its parallel operation.

+4
source share
3 answers

For AsyncTask to work correctly, you must follow several streaming rules:

  • The AsyncTask class must be loaded into the user interface thread. This is done automatically with JELLY_BEAN.

  • A task instance must be created in the user interface thread.

  • execute (Params ...) must be called in the user interface thread.

  • Do not call onPreExecute (), onPostExecute (Result), doInBackground (Params ...), onProgressUpdate (Progress ...) manually.

  • A task can be executed only once (when trying to execute a second execution, an exception will be thrown).

    Additional Information

In my personal opinion, I would suggest using AsyncTask, as it is optimized for background tasks and takes advantage of a multi-core processor.

+2
source

Yes, asynthesis must be running from the user interface thread.

I would suggest using multiple threads / runnables. You will also need to implement a handler in your main activity class to listen for responses from threads.

0
source

You can go either with a thread or with asynchronization. AsyncTask provides you with methods for managing Ui change methods before starting a background task and after completing a backgroung task or making progress.

It depends on the requirements of your architecture.

0
source

All Articles