android--message mechanism

Source: Internet
Author: User

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

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.