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)