14
Hoang Ngo Android Thread, Handler and AsyncTask

Android - Thread, Handler and AsyncTask

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Android - Thread, Handler and AsyncTask

Hoang Ngo

Android

Thread, Handler and AsyncTask

Page 2: Android - Thread, Handler and AsyncTask

Thread

• Act much like usual Java Threads

• Can’t act directly on external User Interface objects (throw the Exception CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views”)

• Can’t be stopped by executing destroy() nor stop(). Use instead interrupt() or join() (by case)

Page 3: Android - Thread, Handler and AsyncTask

Thread

• Two main ways of having a Thread execute application code:– Providing a new class that extends

Thread and overriding its run() method.– Providing a new Thread instance with a

Runnable object during its creation. In both cases, the start() method must be called to actually execute the new Thread.

Page 4: Android - Thread, Handler and AsyncTask

Thread example

Page 5: Android - Thread, Handler and AsyncTask

Handler

• Associated with a single thread and that thread's message queue

• Bound to the thread / message queue of the thread that is creating it

• Deliver messages and runnables to that message queue

• Execute them as they come out of the message queue

Page 6: Android - Thread, Handler and AsyncTask

Handler

• Two main uses for a Handler: – To schedule messages and

runnables to be executed as some point in the future

– To add an action into a queue performed on a different thread

Page 7: Android - Thread, Handler and AsyncTask

Handler example

Page 8: Android - Thread, Handler and AsyncTask

AsyncTask• Created on the UI thread and can be

executed only once

• Run on a background thread and result is published on the UI thread

• The three types used by an asynchronous task are the following– Params, the type of the parameters sent to the

task upon execution – Progress, the type of the progress units published

during the background computation – Result, the type of the result of the background

computation

• Extend AsyncTask<Void, Void, Void>

Page 9: Android - Thread, Handler and AsyncTask

AsyncTask

• Go through 4 steps:– onPreExecute(): invoked on the UI thread

immediately after the task is executed– doInBackground(Param ...): invoked on

the background thread immediately after onPreExecute() finishes executing

– onProgressUpdate(Progress...): invoked on the UI thread after a call to publishProgress(Progress...)

– onPostExecute(Result): invoked on the UI thread after the background computation finishes

Page 10: Android - Thread, Handler and AsyncTask

AsyncTask example

Page 11: Android - Thread, Handler and AsyncTask

• Violate the single thread model: the Android UI toolkit is not thread-safe and must always be manipulated on the UI thread.

• In this piece of code, the ImageView is manipulated on a worker thread, which can cause really weird problems. Tracking down and fixing such bugs can be difficult and time-consuming

Page 12: Android - Thread, Handler and AsyncTask

• Classes and methods also tend to make the code more complicated and more difficult to read.

• It becomes even worse when our implement complex operations that require frequent UI updates

Page 13: Android - Thread, Handler and AsyncTask
Page 14: Android - Thread, Handler and AsyncTask