Handler in Android

Source: Internet
Author: User

Handler in Android
Handler is a lightweight thread in Android. It mainly accepts data sent by sub-threads and uses this data with the main thread to update the UI. it has two functions: (1) scheduling a message or Runnable to execute somewhere in a main thread; (2) arranging an action to be executed in different threads.

When an application is started, Android starts a main thread (that is, the UI thread). The main thread is the UI control in the management interface for event distribution. For example, if you click a Button, Android will distribute the event to the Button to respond to your operation. If you need a time-consuming operation, such as reading data online or reading a large local file, you cannot put these operations in the main thread, if you put it in the main thread, ANR will appear on the interface. If it has not been completed in more than five seconds, you will receive an error saying "Force disable" in the Android system ". at this time, we need to put these time-consuming operations in a sub-thread, because the sub-thread does not involve UI updates, that is, the update UI can only be updated in the main thread, and the operations in the sub-thread are dangerous. at this time, Handler came up to solve this complicated problem. Because Handler runs in the main thread (in the UI thread), it and the sub-thread can transmit data through the Message object, at this time, Handler is responsible for receiving the Message object (containing data) transmitted by the subthread using the sedMessage () method ), put these messages into the main thread queue and update the UI with the main thread.


Let's look at an instance, interact with the network in the Child thread, and then update the data returned by the server on the UI.

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. logoff; import android. OS. message; import android. util. log;/*** process the Door event request and update the corresponding main UI thread * @ author liyangchao **/public class DoorHanlder extends Handler {private static final String TAG = DoorHanlder. class. getSimpleName (); private MainActivity mainActivity; private DoorThread doorThread; public DoorHanlder () {// TODO Auto-generated constructor stub} public DoorHanlder (low.l) {super (L );} public DoorHanlder (MainActivity mainActivity) {super (); this. mainActivity = mainActivity; doorThread = new DoorThread (mainActivity); doorThread. start () ;}@ Overridepublic void handleMessage (Message msg) {// TODO Auto-generated method stubLog. d (TAG, "DoorHandler starting handler message ..... "); super. handleMessage (msg); // update UIBundle bundle = msg. getData (); String door_detail = bundle. getString ("door"); mainActivity. updateDoorDetails (door_detail );}}

Check the Thread code corresponding to the Handler.

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 (); private 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 (1000);} catch (Exception e) {// TODO: handle finished tione. printStackTrace ();} Log. d (TAG, "doorThread starting ....... "); 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 );}}

OK. Now you can call it in the main thread. You only need one sentence;

doorHanlder = new DoorHanlder(this);


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.