Asynchronous Tasks with Android Anthony Dahanne, Ing Jr identi.ca/twitter : @anthonydahanne blog : ...

10
Asynchronous Tasks with Android Anthony Dahanne, Ing Jr identi.ca/twitter : @anthonydahanne blog : http://blog.dahanne.net Android Montreal, le 01/09/2010

Transcript of Asynchronous Tasks with Android Anthony Dahanne, Ing Jr identi.ca/twitter : @anthonydahanne blog : ...

Page 1: Asynchronous Tasks with Android Anthony Dahanne, Ing Jr identi.ca/twitter : @anthonydahanne blog :  Android Montreal, le 01/09/2010.

Asynchronous Tasks with Android

Anthony Dahanne, Ing Jridenti.ca/twitter : @anthonydahanneblog : http://blog.dahanne.net

Android Montreal, le 01/09/2010

Page 2: Asynchronous Tasks with Android Anthony Dahanne, Ing Jr identi.ca/twitter : @anthonydahanne blog :  Android Montreal, le 01/09/2010.

Content

Why should I care about multi threading ? What are the options ? AsyncTasks to the rescue ! AsyncTask API : the lifecycle AsyncTask API : real life example (demo) Q&A

Page 3: Asynchronous Tasks with Android Anthony Dahanne, Ing Jr identi.ca/twitter : @anthonydahanne blog :  Android Montreal, le 01/09/2010.

Why should I care about threading ? Remote calls (Http) Intense computing (media manipulation)

One thread =

Frustrated user !

Page 4: Asynchronous Tasks with Android Anthony Dahanne, Ing Jr identi.ca/twitter : @anthonydahanne blog :  Android Montreal, le 01/09/2010.

Why should I care about threading ? Remote calls (Http) Intense computing (media manipulation)

Many threads=

Happy user

Page 5: Asynchronous Tasks with Android Anthony Dahanne, Ing Jr identi.ca/twitter : @anthonydahanne blog :  Android Montreal, le 01/09/2010.

What are the options ?

Or you can try THE REAL THING !

Handler based API :Activity.runOnUiThread(Runnable)

View.post(Runnable)

View.postDelayed(Runnable, long)

And .... Handler ! (android.os.Handler)

Page 6: Asynchronous Tasks with Android Anthony Dahanne, Ing Jr identi.ca/twitter : @anthonydahanne blog :  Android Montreal, le 01/09/2010.

AsyncTasks to the rescue !Asynctask API :

Something doInBackground(String... params) {

return null;}

protected void onPreExecute() {}

protected void onPostExecute(Something b) {

}

protected void onProgressUpdate(Ty... values) {

}

The best way to round kick your threads !

Page 7: Asynchronous Tasks with Android Anthony Dahanne, Ing Jr identi.ca/twitter : @anthonydahanne blog :  Android Montreal, le 01/09/2010.

Asynchronous Tasks : the lifecycle

Page 8: Asynchronous Tasks with Android Anthony Dahanne, Ing Jr identi.ca/twitter : @anthonydahanne blog :  Android Montreal, le 01/09/2010.

AsyncTask : real life example

Page 9: Asynchronous Tasks with Android Anthony Dahanne, Ing Jr identi.ca/twitter : @anthonydahanne blog :  Android Montreal, le 01/09/2010.

The end ! Q&A !

Page 10: Asynchronous Tasks with Android Anthony Dahanne, Ing Jr identi.ca/twitter : @anthonydahanne blog :  Android Montreal, le 01/09/2010.

References AsyncTasksDemo

http://github.com/anthonydahanne/asynctasksdemo

G2Android, the Gallery2 client for Android

http://code.google.com/p/g2android/

Painless threading, by Romain Guy

http://android-developers.blogspot.com/2009/05/painless-threading.html

Multithreading For Performance, by Gilles Debunne

http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html

Photos :

Slide 5 : http://www.flickr.com/photos/miggs43/4478547596/

Slide 6 :http://itsguycode.com/images/stories/chuck-norris/chuck-norris-thumbs-up.jpg

Video :

Slide 9 : http://www.youtube.com/watch?v=_hZrXdJ-ibo&NR=1