Link Address: http://www.cnblogs.com/lknlfy/archive/2012/02/19/2358155.html
I. Overview
Handler This class is primarily used to send and process messages. It has multiple send message functions, but only one processing message function handlemessage ().
Second, the requirements
There are two threads in the program, one is the UI thread, one is the thread that you created, a message is sent in the thread you created, and the message is received and processed in the UI thread.
Third, the realization
Not only Android, but many other interface programming is not allowed to update the UI interface directly in other threads. You can therefore send a message to the UI thread in other threads that needs to update the interface, allowing the UI thread to update the interface.
New Project MyHandler, modify the/res/layout/main.xml layout file, add 2 button buttons inside, 1 empty view (mainly for typesetting) and a text textview. The complete Main.xml file is as follows:
1 <?xml version= "1.0" encoding= "Utf-8"?>
2 <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
3 android:layout_width= "Fill_parent"
4 android:layout_height= "Fill_parent"
5 android:orientation= "Vertical" >
6
7 <button
8 android:id= "@+id/mbutton"
9 android:layout_width= "Fill_parent"
Ten android:layout_height= "Wrap_content"
android:text= "Boot Thread"
Android:textsize= "15px"
/>
14
<button
Android:id= "@+id/sbutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Stop Thread"
Android:textsize= "15px"
/>
22
<view
Android:layout_width= "Fill_parent"
android:layout_height= "70px"
/>
27
<textview
Android:id= "@+id/mtextview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:text= ""
Android:textcolor= "#FF0000"
Android:textsize= "50px"
Android:gravity= "Center_horizontal"
Approx./>
37
</LinearLayout>
Then modify the Myhandleractivity.java file, which defines 2 classes, one is the Mhandler class, inherits from the handler class, implements the Handlemessage (Message msg) function. The other is the Mthread class, which inherits from the thread class and implements the run () function. Other those in the program have detailed comments, the complete Myhandleractivity.java file is as follows:
1 package Com.nan.handler;
2
3 Import android.app.Activity;
4 Import Android.os.Bundle;
5 Import Android.os.Handler;
6 Import Android.os.Message;
7 Import Android.view.View;
8 Import Android.widget.Button;
9 Import Android.widget.TextView;
10
public class Myhandleractivity extends Activity
12 {
13//Thread stop flag bit
The private Boolean STOP = true;
15//thread has been started for a few seconds
+ private int second = 0;
17
Private Mhandler handler;
Private Mthread thread;
Private Button Mbutton;
+ Private Button Sbutton;
The private TextView Mtextview;
23
/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate)
27 {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
30
Handler = new Mhandler ();
thread = new Mthread ();
33
Mtextview = (TextView) Findviewbyid (R.id.mtextview);
Mbutton = (Button) Findviewbyid (R.id.mbutton);
36//Set "Start Thread" button event handling
Panax Notoginseng Mbutton.setonclicklistener (New View.onclicklistener ()
38 {
39
@Override
$ public void OnClick (View v)
42 {
+//TODO auto-generated method stub
44//Set flag bit
A-STOP = false;
46//Opening a new thread
Thread.Start ();
48
49}
50});
Wuyi Sbutton = (Button) Findviewbyid (R.id.sbutton);
52//Set "Stop Thread" button event handling
Sbutton.setonclicklistener (New View.onclicklistener ()
54 {
55
@Override
$ public void OnClick (View v)
58 {
/TODO auto-generated Method stub
60//Set flag bit
A-STOP = true;
62}
63});
64
65}
66
67//self-defined handler class
The private class Mhandler extends Handler
69 {
@Override
The public void Handlemessage (Message msg)
72 {
Switch (msg.what)
74 {
Case 1:
76 {
77//S increase
Second + +;
79//Display is the first few seconds
Mtextview.settext (integer.tostring (second));
Bayi break;
82}
83}
84}
85}
86
87//self-defined thread class
Private class Mthread extends Thread
89 {
90
@Override
92//execute this function when thread starts
public void Run ()
94 {
95//Loop until the flag is "true"
(! STOP)
97 {
98 try {
99//Delay 1 seconds
Thread.Sleep (1000);
101} catch (Interruptedexception e) {
102//TODO auto-generated catch block
103 E.printstacktrace ();
104}
A message msg = new Message ();
106//Message Flag
107 msg.what = 1;
108//Send this message
109 Handler.sendmessage (msg);
110}
111}
112}
113
114}
OK, run the program.
Click on the "Open Thread" button, as follows:
Click on the "Stop thread" button and find that the count is stopped, indicating that the newly opened thread has exited.
Complete.
Android Application Development Basics (5)-----Handler and multithreading