Handler primarily accepts data sent by a child thread and updates the UIwith this data in conjunction with the main thread.
When the application starts,Android first opens a main thread (that is, the UI thread), and the main thread is the UI control in the admin 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: when the network reads data or reads 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 suspended animation, if 5 seconds has not been completed, will receive an error "forced shutdown" on the Android system. This time we need to put these time-consuming operations on a sub-thread, because the child threads involve UI updates, the Android main thread is not secure , 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 is running in the mainline approached (UI thread), it and the child thread can pass the data through the message object, and at this time, the handler undertakes to accept the child thread passed 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.
Let's take a look at the simple way to use handler.
Package Com.example.android_handler_textview;import Android.os.bundle;import Android.os.handler;import Android.os.message;import Android.app.activity;import Android.view.menu;import Android.view.View;import Android.widget.button;import Android.widget.textview;public class Mainactivity extends Activity {private button button ;p rivate static TextView textview;private MyHandler handler; @Overrideprotected void OnCreate (Bundle savedinstancestate {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); button = (Button) This.findviewbyid ( R.id.button1); TextView = (TextView) This.findviewbyid (r.id.msg); handler = new MyHandler (); Button.setonclicklistener ( New View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stubnew Thread (New Mythrea D ()). Start ();}}); public static class MyHandler extends Handler {@Overridepublic void Handlemessage (Message msg) {//TODO auto-generated Me Thod stubsuper.handlemessage (msg); int arg1 = MSG.ARG1; String Name= (String) msg.obj;textview.append (name + arg1);}} public class MyThread implements Runnable {int count = 0; @Overridepublic void Run () {//TODO auto-generated method Stubwhi Le (Count <=) {try {thread.sleep ($);} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();} Gets the message from the message pool, if there is no message, creates a message, if any, then extracts the message to carry the data, sent by handler a message message = Message.obtain (); message.arg1 = count; Message.obj = "Jack"; count++;handler.sendmessage (message);}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}