Android message mechanism (i)

Source: Internet
Author: User

Message Manipulation Class Handler

First write the Main.xml file code as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:orientation=" vertical "    android:layout_width=" Fill_parent    " android:layout_height= "Fill_parent" ><textview     android:id= "@+id/info" android:layout_width= "Fill_    Parent    

Then rewrite the Activity.java class

The code is as follows:

Package Com.example.myfirstproject;import Java.util.arraylist;import Java.util.list;import java.util.Timer;import Java.util.timertask;import Android.os.bundle;import Android.os.handler;import Android.os.Message;import Android.app.activity;import Android.content.pm.activityinfo;import Android.content.res.configuration;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.ViewGroup Import android.widget.*;p ublic class Mainactivity extends Activity {private static int count = 0;public static final int SET = 1;private Handler MyHandler = new Handler () {public void Handlemessage (android.os.Message msg) {switch (msg.what) { Case SET:MainActivity.this.info.setText ("mldn-" +count++);}};    Private TextView info = null;        public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        This.info = (TextView) Super.findviewbyid (r.id.info); Timer timer = new TimER ();    Timer.schedule (New MyTask (), 0, 1000);    } private class MyTask extends timertask{public void run () {message msg = new Message ();    Msg.what = SET;    MainActivity.this.myHandler.sendMessage (msg); }    }}

Operating effect:

Android message mechanism (i)

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.