1. Message
Messages, which are understood as data units for inter-thread communication. For example, a background thread that needs to update the UI after processing the data can send a message containing the update information to the UI thread.
2. Handler
Handler is the primary processor for message, which is responsible for adding messages to message queues and processing messages in message queues.
3. Looper
The circulator, acting as a bridge between the message queue and the handler, loops out the message within the message queue and delivers it to the corresponding handler for processing.
Please refer to the following example:
The. XML code is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" > <textview android:id= "@+id/info" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/></linearlayout>
. Java code is as follows:
Package Org.lxh.demo;import Java.util.timer;import Java.util.timertask;import android.app.activity;import Android.app.alertdialog;import Android.app.dialog;import Android.content.dialoginterface;import Android.os.Bundle ; Import Android.os.handler;import Android.os.message;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.view.onfocuschangelistener;import Android.widget.Button; Import Android.widget.edittext;import Android.widget.textview;public class Hello extends Activity {public static int Count = 1;public static final int SET = 1;private TextView msg = null;private Handler myhandle = new Handler () {@Overridep ublic void Handlemessage (Message msg) {//overwrite this method switch (msg.what) {//Judgment action type case SET:Hello.this.msg.setText ("mldn-" + count++);}}; public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//life cycle Method Super.setcontentview ( R.layout.main); Set the layout manager to use this.msg = (TextView) Super.findviewbyid (r.id.info); Timer timer = new TimeR ();//define Scheduler Timer.schedule (new MyTask (), 0, 1000);//0 means start now, 1000 means interval is one second}private class MyTask extends TimerTask {@ overridepublic void Run () {//start thread Message msg = new Message (); msg.what = SET; Hello.this.myHandle.sendMessage (msg);}}}
The following example runs:
android--message mechanism