Asynctask From Non Ui Thread
Solution 1:
There are a few threading rules that must be followed for AsyncTask to work properly:
The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.
The task instance must be created on the UI thread.
execute(Params...) must be invoked on the UI thread.
Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.
The task can be executed only once (an exception will be thrown if a second execution is attempted.)
In my personal opinion, I would suggest using AsyncTask as it is highly optimized for running background tasks and exploit benefits like multicore processor.
Solution 2:
Yes, asynctask needs to be run from the UI thread.
I'd suggest to use multiple threads/runnables. You'll also need to implement a handler in your main activity class to listen to the responses from the threads.
Solution 3:
You can go with either thread or asynctask. AsyncTask provide you methods to manage Ui changes methods before starting background task and after finishing backgroung task or getting its progress.
It depends on your architecture requirement.
Post a Comment for "Asynctask From Non Ui Thread"