Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15309728
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类: Android平台

2012-12-15 11:26:00

e文的,文章描述的很清楚,转载过来,希望对大家有帮助

It’s widely known that it’s illegal to update UI components directly from threads other than main thread in android. This android document (Handling Expensive Operations in the UI Thread) suggests the steps to follow if we need to start a separate thread to do some expensive work and update UI after it’s done. The idea is to create a Handler object associated with main thread, and post a Runnable to it at appropriate time. This Runnable will be invoked on the main thread. This mechanism is implemented with Looper and Handler classes.

 

The Looper class maintains a MessageQueue, which contains a list messages. An important character of Looper is that it’s associated with the thread within which the Looper is created. This association is kept forever and can’t be broken nor changed. Also note that a thread can’t be associated with more than one Looper. In order to guarantee this association, Looper is stored in thread-local storage, and it can’t be created via its constructor directly. The only way to create it is to call prepare static method on Looper. prepare method first examines ThreadLocal of current thread to make sure that there isn’t already a Looper associated with the thread. After the examination, a new Looper is created and saved in ThreadLocal. Having prepared the Looper, we can call loop method on it to check for new messages and have Handler to deal with them.

As the name indicates, the Handler class is mainly responsible for handling (adding, removing, dispatching) messages of current thread’s MessageQueue. A Handler instance is also bound to a thread. The binding between Handler and Thread is achieved via Looper and MessageQueue. A Handler is always bound to a Looper, and subsequently bound to the thread associated with the Looper. Unlike Looper, multiple Handler instances can be bound to the same thread. Whenever we call post or any methods alike on the Handler, a new message is added to the associated MessageQueue. The target field of the message is set to current Handler instance. When the Looper received this message, it invokes dispatchMessage on message’s target field, so that the message routes back to to the Handler instance to be handled, but on the correct thread.

The relationships between Looper, Handler and MessageQueue is shown below:

his design is very similar to win32′s message loop. The benefit of this design is that we no longer need to worry about concurrency issues while manipulating UI elements because they are guaranteed to be manipulated on the same thread. Without this simplicity, our code may bloat heavily because we have to lock access to UI elements whenever they are possibly accessed concurrently.

 

The example at the end of this post shows how to send messages to different handlers associated with main Looper (Main Looper is created via prepareMainLooper in ActivityThread.main).

 

Example:

… looper_and_handler/

阅读(3940) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~