Thread
- UI Thread - Is the main thread
- UI Toolkit is not thread-safe
Methods
The following methods allows to run in the UI Thread
- View.post(Runnable r)
- Activity.runOnUiThread(Runnable r)
AsyncTask
Manage the Backgroud Thread & UI Thread
Used for relative short tasks that lasting few seconds
Used for relative short tasks that lasting few seconds
- Background Thread
- Performs work
- Indicate progress
- UI Thread
- Does Setup
- Publishes intermediate progress
- Uses Results
Workflow
- onPreExecute()
- Run in UI Thread before doInBackground()
- Do the setup
- doInBackground()
- Do the work in the background
- Return the result in a Result object
- pulishProgress()
- This method may be invoked
- onProgressUpdate()
- If pulishProgress() is invoked then this method continue
- Runs in the UI Thread
- onPostExecute
- Runs after doInBackground()
Handler
- Handle two threads of any kind
- One thread can hand off work to another thread by messages & posting runnables to handler associated with the second thread
Elements
- Runnable
- Contains an instance of the Runnable instance
- Sender implements response
- Message
- Contains
- Message code
- Object & Integer arguments
Comentarios
Publicar un comentario