Why?
Because UI operations are not thread-safe in an Android system, it can cause thread-safety problems if multiple threads concurrently operate on the same component. To solve this problem,
Android has a rule that allows only UI threads to modify the properties of the UI component, which means that a single-threaded model is required, which results in a long-time-consuming data in the UI interface
Updates and so on will form the program of suspended animation is the ANR exception, if not completed in 20 seconds the program will be forced to close. So, for example, when another thread modifies the UI component,
You need to use the handler messaging mechanism.
Use Status?
1. Sending messages to a newly-started thread
2. Get and process messages on the main thread
Handler the execution of the process
Role Description:
1) Looper: A thread can produce a Looper object that manages this line thread the message Queue
2) Handler: You can construct Handler objects to communicate with Looper in order to push new messages into the message queue, or
Receives Looper messages sent from the message Queue.
3) Message queue (Message Queuing): Is the message used to hold the thread into place.
4) Thread: The UI thread is usually the main thread, and the Android launcher will create a message Queue for it.
Each thread can contain a Looper object and a Message Queue data structure. In your application,
You can define Handler subcategories to receive messages sent by Looper.
Detail?
handle several ways to send and process messages:
1. void handlemessage (Message msg): The method that processes the message, which is usually overridden.
2.final boolean hasmessage (int what): Checks whether Message Queuing contains a message with the What property is the specified value
3.final boolean hasmessage (int what, Object object): Checks whether Message Queuing contains a message with a value specified by the What's good Object property
4.sendEmptyMessage (int): Send an empty message
5.final Boolean send emptymessagedelayed (int what, long Delaymillis): Specifies how many milliseconds to send an empty message
6.final boolean sendMessage (Message msg): Send Message now
7.final boolean sendmessagedelayed (Message Msg,long Delaymillis): Sends a message after a few seconds
Demo:
Private StaticString image_path = "Http://ww4.sinaimg.cn/bmiddle/786013a5jw1e7akotp4bcj20c80i3aao.jpg"; PrivateImageView imageview_1; PrivateButton button_1; PrivateButton button_2;
//handler define and override the Handlemessage method PrivateHandler Handler =NewHandler () {@Override Public voidhandlemessage (android.os.Message msg) {if(Msg.what = = 1) {System.out.println (">>>>>step2"); byte[] data = (byte[]) msg.obj; Bitmap BMP= Bitmapfactory.decodebytearray (data, 0, data.length); Imageview_1.setimagebitmap (BMP); } } }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_choose); Button_1=(Button) Findviewbyid (R.id.yes); Imageview_1=(ImageView) Findviewbyid (R.id.imageview); Button_1.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {NewThread (NewRunnable () {@Override Public voidrun () {HttpClient HttpClient=Newdefaulthttpclient (); HttpGet HttpGet=NewHttpGet (image_path); HttpResponse HttpResponse=NULL; Try{Thread.Sleep (10000); HttpResponse=Httpclient.execute (HttpGet); byte[] data =Entityutils.tobytearray (Httpresponse.getentity ());
//define message and pass message to handler Message msg=Message.obtain (); Msg.what=1; Msg.obj=data; System.out.println (">>>>>step1"); Handler.sendmessage (msg); } Catch(Exception e) {e.printstacktrace (); }}). Start (); } }); }
Effect:
Reference Blog: http://blog.csdn.net/x605940745/article/details/13001279
Http://www.cnblogs.com/youxilua/archive/2011/11/25/2263825.html
android--Multi-Thread handler