Http://www.cnblogs.com/plokmju/p/android_Handler.html
Android does not allow time-consuming operations in the main thread, such as network operations, to avoid ANR
ANR (application not responding)
Http://baike.baidu.com/link?url=rLzKRNkjt79XITQKhRXp32alhsuKEt2FoHPw3vuB2UlEvyKOZwnEh4OYoPy4_fwO6zPPECXWre4ycip4mB0LOq
Activity should create as little as possible in its critical life-cycle methods (such as OnCreate () and Onresume ()). Potentially time-consuming operations, such as network or database operations, or high time-consuming computations such as changing the bitmap size, should be done in the thread of the Strand (or, in the case of database operations, through asynchronous requests).
By default, the maximum execution time for activity in Android is 5 seconds, and the maximum execution time for Broadcastreceiver is 10 seconds.
so if you want to do this, you should turn on a sub-thread. in a child thread, Android does not allow UI manipulation. If you want to perform UI operations in a child thread, you can use handler to open the UI thread.
Handler, which inherits directly from object, a Handler allows the sending and processing of a message or Runnable object and is associated to the MessageQueue of the main thread. Each handler has a separate thread and is associated to a thread of Message queuing, meaning that a handler has an intrinsic message queue. When an handler is instantiated, it is hosted on a thread and message queue thread, which handler messages or runnable into the message queue and extracts messages or runnable from the message queue to manipulate them.
From the above, there are two usages of handler:
- Post:post allows a Runnable object to be enqueued into the message queue. It has the following methods: Post (Runnable), Postattime (Runnable,long), postdelayed (Runnable,long).
- Sendmessage:sendmessage allows a Message object containing the message data to be pressed into the queue of messages. Its methods are: sendemptymessage (int), sendMessage (message), Sendmessageattime (Message,long), sendmessagedelayed (message, Long).
See the first linked article for a specific usage
http://blog.csdn.net/gh102/article/details/7191486
This way, the use of post is a little clearer.
Post and message differences:
http://blog.csdn.net/u013168615/article/details/47024073
As can be seen from the source code, post is called the Sendmessagedelayed method:
Public Final Boolean post (Runnable R) { return 0 ); }
And the getpostmessage is to wrap runnable R into an empty message and return
Private Static Message Getpostmessage (Runnable r) { = message.obtain (); = R; return m;}
So there's no essential difference between post and message, it's just a different usage.
Handler.post and Handler.sendmessage are essentially indistinguishable, sending a message to the message queue, and both Message Queuing and handler are dependent on the same thread.
android-Multithreading Handler