I have two questions:
onPreExecute()
, onProgressUpdate(Progress...)
and onPostExecute(Result)
are invoked on the UI thread?I wanna know that because I have a TCP connection running on a worker thread and when a packet is received I wanna start a new thread to make the parse of this packet and after that refresh some data structures on the UI thread.
Thanks in advance!
The AsyncTasks are heavily using the ThreadPoolExecutors to do its tasks on the worker thread, and once done it passes back the result to the UI thread using Handlers.
Android App Development for Beginners Android AsyncTask going to do background operation on background thread and update on main thread. In android we cant directly touch background thread to main thread in android development. asynctask help us to make communication between background thread to main thread.
Run() or similar constructs, a task runs on a separate thread (mostly a managed thread-pool one), managed by the . NET CLR. But that depends on the actual implementation of the task.
So resuming: execute() calls executeOnExecutor() and we get a queue of tasks that wait for execution while just one Task is running in the executor, all of this applicationwise. This means that even though we call 10 AsyncTasks execute() one will be executing while the others will all be enqueued.
From the Android AsyncTask doc:
"The task instance must be created on the UI thread.", and
"execute(Params...) must be invoked on the UI thread."
So I think the answer to your first question is "no". As far as the second, those methods are all invoked on the UI thread, but it's a bit moot.
EDIT: I'm not sure if those are absolute restrictions, or strong suggestions, but in both cases, I'd recommend following them.
Just for the Record: If you start a AsyncTask outside the UI-Thread the onPreExecute will not be executed from the UI-Thread (but from the caller Thread). This will lead to an exception. However the onPostExecute Method will always be executed on the UI-Thread. Hope to help someone :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With