when to use handler in android

If the programmer does not use any concurrency constructs, all code of an Android application runs in the main thread and every statement is executed after each other.
To provide a good user experience all potentially slow running operations in an Android application should run asynchronously, e.g. via some way of concurrency constructs of the Java language or the Android framework. This includes all potential slow operations, like network, file and database access and complex calculations.

1. Using Java threading in Android
Android supports the usage of the Thread class to perform asynchronous processing.

Android also supplies the java.util.concurrent package to perform something in the background, e.g. using the ThreadPools and Executor classes.

If you need to update the user interface from a new Thread, you need to synchronize with the user interface thread.
2.Concurrency constructs in Android
Android provides additional constructs to handle concurrently in comparison with standard Java. You can use the android.os.Handler class or the AsyncTasks classes. More sophisticated approach are based on the Loader class, retained Fragments and services.

Purpose of the Handler class
The Handler class can be used to register to a thread and provides a simple channel to send data to this thread.

A Handler object registers itself with the thread in which it is created. For example, if you create a new instance of the Handler class in the onCreate() method of your activity, the resulting Handler object can be used to post data to the main thread.

The data which can be posted via the Handler class can be an instance of the Message or the Runnable class.

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: