android.os.AsyncTask

Android modifies the user interface via one thread, the so called UI Thread.

If you perform a long running operation directly on the UI Thread, for example downloading a file from the internet, the user interface of your application will “freeze” until the corresponding task is finished.

To provide a good user experience all long running operations in an Android application should run asynchronously by using the AsyncTask class.

AsyncTask class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. 

Private class ShowDialogAsyncTask extends AsyncTask<Params, Progress, Result>{....}

AsyncTask has three generic type

  • Params: input, what you pass to the AsyncTask.
  • Progress: if you have any updates, passed to onProgressUpdate()
  • Result: the output. what returns doInBackground()

The most common methods you will need to implement are these:

   1. onPreExecute() –

  • called on the UI thread before the thread starts running, usually used to setup the task, for example by displaying a progress bar.

   2. doInBackground(Params…)

  • this is the method that runs on the background thread. In this method you should put all the code you want the application to perform in background. e.g., downloads the XML feed and does the parsing.
  • The doInBackground() is called immediately after onPreExecute().
  • When it finishes, it sends the result to the onPostExecute().

   3. onProgressUpdate() – called when you invoke publishProgress() in the doInBackground().

   4. onPostExecute(Result) – called on the UI thread after the background thread finishes. It takes as parameter the result received from doInBackground().

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: