handler is a lightweight thread in Android that primarily accepts data sent by child threads and updates the UI with this data in conjunction with the main thread. it has two functions: (1): Schedule a message or runnable to execute somewhere in a main thread, (2) Schedule an action to be executed in a different thread .
When the application starts, Android first turns on a main thread (i.e. the UI thread), the main thread is the UI control in the management interface for event distribution, for example, if you click on a button, Android will distribute the event to the button to respond to your action. If you need a time-consuming operation at this time, for example: network read data, or read a large local file, you can not put these operations in the main thread, if you put in the main thread, the interface will appear ANR, if more than 5 seconds have not been completed, will receive an Android system error mention Force Close. This time we need to put these time-consuming operations on a sub-thread because the child threads do not involve UI updates, that is, the update UI can only be updated in the main thread, and the operations in the child threads are dangerous. At this time, handler appeared to solve this complex problem, because handler runs in the mainline approached (UI thread), it and the child thread can pass the data through the Message object, at this time, the handler undertakes to accept the child thread to pass over ( The child thread uses the Sedmessage () method to transmit the message object, which contains data, which is placed in the main thread queue and updated with the main thread.
Take a look at an instance, interact with the network in a child thread, and then update the data returned by the server on the UI.
<span style= "FONT-SIZE:14PX;" >package Com.smarthouse.handler;import Com.smarthouse.activity.mainactivity;import Com.smarthouse.activity.R; Import Com.smarthouse.thread.doorthread;import Android.os.bundle;import Android.os.handler;import Android.os.looper;import android.os.message;import android.util.log;/** * Handles door event requests and updates the corresponding main UI thread * @author Liyangchao * */public class Doorhanlder extends handler{private static final String TAG = DoorHanlder.class.getSimpleName ();p rivate Ma Inactivity Mainactivity;private doorthread doorthread;public Doorhanlder () {//TODO auto-generated constructor stub} Public Doorhanlder (Looper l) {super (L);} Public Doorhanlder (Mainactivity mainactivity) {super (); this.mainactivity = Mainactivity;doorthread = new DoorThread ( mainactivity);d Oorthread.start ();} @Overridepublic void Handlemessage (Message msg) {//TODO auto-generated method Stublog.d (TAG, "Doorhandler starting Handl ER message ... "); Super.handlemessage (msg);//update Uibundle bundle = Msg.getdata (); String Door_detail = bundle.getstring ("door"); Mainactivity.updatedoordetails (Door_detail);}} </span>
Look at the code for this handler corresponding thread
<span style= "FONT-SIZE:14PX;" >package com.smarthouse.thread;import android.os.bundle;import Android.os.message;import android.util.Log; Import Com.smarthouse.activity.mainactivity;import Com.smarthouse.dealer.dealrequest;public class DoorThread Extends thread{private static final String TAG = DoorThread.class.getSimpleName ();p rivate mainactivity mainactivity;// Private Countdownlatch countdownlatch;private String result = Null;public Doorthread (mainactivity mainactivity) { this.mainactivity = Mainactivity;//countdownlatch = new Countdownlatch (1);} @Overridepublic void Run () {try {thread.sleep ()} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();} LOG.D (TAG, "doorthread starting ..."); <span style= "font-family:arial, Sans-serif, Helvetica, Tahoma;" > </span>dealrequest dealrequest = new Dealrequest (); result = Dealrequest.getcontentfromurl ("http://www.reveriedream.com/test/appget3.asp"); Message msg = new Message (); Bundle bundle =New Bundle ();//Store Data bundle.putstring ("door", result); Msg.setdata (bundle); Mainactivity.getdoorhanlder (). SendMessage (msg);}} </span>
OK, now go to the main thread to call it, just need a word;
<span style= "FONT-SIZE:14PX;" >doorhanlder = new Doorhanlder (this);</span>
Handler in Android