Flexible application of the Message mechanism in the Android Series

Source: Internet
Author: User

The android instance is used to display the corresponding functions. The program interface is as follows:

The program code is as follows:

1 package com. android. messageexample;
2 import android. app. Activity;
3 import android. content. Context;
4 import android. graphics. Color;
5 import android. OS. Bundle;
6 import android. OS. Handler;
7 import android. OS. logoff;
8 import android. OS. Message;
9 import android. util. Log;
10 import android. view. View;
11 import android. view. View. OnClickListener;
12 import android. widget. Button;
13 import android. widget. LinearLayout;
14 import android. widget. TextView;
15 public class MessageExample extends Activity implements OnClickListener {
16 private final int WC = LinearLayout. LayoutParams. WRAP_CONTENT;
17 private final int FP = LinearLayout. LayoutParams. FILL_PARENT;
18 public TextView TV;
19 private EventHandler mHandler;
20 private Handler mOtherThreadHandler = null;
21 private Button btn, btn2, btn3, btn4, btn5, btn6;
22 private NoLooperThread noLooerThread = null;
23 private OwnLooperThread ownLooperThread = null;
24 private ReceiveMessageThread receiveMessageThread = null;
25 private Context context = null;
26 private final String sTag = "MessageExample ";
27 private boolean postRunnable = false;
28
29/** Called when the activity is first created .*/
30 @ Override
31 public void onCreate (Bundle savedInstanceState ){
32 super. onCreate (savedInstanceState );
33 context = this. getApplicationContext ();
34 LinearLayout layout = new LinearLayout (this );
35 layout. setOrientation (LinearLayout. VERTICAL );
36 btn = new Button (this );
37 btn. setId (101 );
38 btn. setText ("message from main thread self ");
39 btn. setOnClickListener (this );
40 LinearLayout. LayoutParams param =
41 new LinearLayout. LayoutParams (250,50 );
42 param. topMargin = 10;
43 layout. addView (btn, param );
44 btn2 = new Button (this );
45. btn2.setId (102 );
46 btn2.setText ("message from other thread to main thread ");
47 btn2.setOnClickListener (this );
48 layout. addView (btn2, param );
49 btn3 = new Button (this );
50 btn3.setId (103 );
51 btn3.setText ("message to other thread from itself ");
52 btn3.setOnClickListener (this );
53 layout. addView (btn3, param );
54 btn4 = new Button (this );
55. btn4.setId (104 );
56 btn4.setText ("message with Runnable as callback from other thread to main thread ");
57 btn4.setOnClickListener (this );
58 layout. addView (btn4, param );
59 btn5 = new Button (this );
60. btn5.setId (105 );
61 btn5.setText ("main threads message to other thread ");
62 btn5.setOnClickListener (this );
63 layout. addView (btn5, param );
64 btn6 = new Button (this );
65 btn6.setId (106 );
66 btn6.setText ("exit ");
67 btn6.setOnClickListener (this );
68 layout. addView (btn6, param );
69 TV = new TextView (this );
70 TV. setTextColor (Color. WHITE );
71 TV. setText ("");
72 LinearLayout. LayoutParams param2 =
73 new LinearLayout. LayoutParams (FP, WC );
74 param2.topMargin = 10;
75 layout. addView (TV, param2 );
76 setContentView (layout );
77
78 // The main thread needs to send a message to the other thread. The other thread is created here.
79 receiveMessageThread = new ReceiveMessageThread ();
80 receiveMessageThread. start ();
81}
82
83 // implement the OnClickListener interface
84 @ Override
85 public void onClick (View v ){
86 switch (v. getId ()){
87 case 101:
88 // The main thread sends a message to itself
89 logoff;
90 Loopers = loopers. myloopers (); // get the Main Loopers related with the main thread
91 // if no parameter is provided, The logoff value of the current thread is assigned to the mlogoff member in the Handler using the logoff value corresponding to the current thread (here the Main logoff value is used ).
92 mHandler = new EventHandler (logoff );
93 // mHandler = new EventHandler ();
94 // clear the message in the entire MessageQueue
95 mHandler. removeMessages (0 );
96 String obj = "This main threads message and received ed by itself! ";
97 // get the Message object
98 Message m = mHandler. obtainMessage (1, 1, 1, obj );
99 // send the Message object to the MessageQueue of main thread
100 mHandler. sendMessage (m );
101 break;
102 case 102:
103 // The other thread sends a message to the main thread
104 postRunnable = false;
105 noLooerThread = new NoLooperThread ();
106 noLooerThread. start ();
107 break;
108 case 103:
109 // other thread obtains the message sent by itself
110 TV. setText ("please look at the error level log for other thread received message ");
111 ownLooperThread = new OwnLooperThread ();
112 ownLooperThread. start ();
113 break;
114 case 104:
115 // other thread sends messages to the main thread through Post Runnable
116 postRunnable = true;
117 noLooerThread = new NoLooperThread ();
118 noLooerThread. start ();
119 break;
120 case 105:
121 // The main thread sends a message to the other thread
122 if (null! = MOtherThreadHandler ){
123 TV. setText ("please look at the error level log for other thread received ed message from main thread ");
124 String msgObj = "message from mainThread ";
125 Message mainThreadMsg = mOtherThreadHandler. obtainMessage (1, 1, 1, msgObj );
126 mOtherThreadHandler. sendMessage (mainThreadMsg );
127}
128 break;
129 case 106:
130 finish ();
131 break;
132}
133}
134 class EventHandler extends Handler
135 {
136 public EventHandler (low.logoff ){
137 super (logoff );
138}
139 public EventHandler (){
140 super ();
141}
142 public void handleMessage (Message msg ){
143 // different processes can be executed based on msg. what, which is not implemented here
144 switch (msg. what ){
145 case 1:
146 TV. setText (String) msg. obj );
147 break;
148 case 2:
149 TV. setText (String) msg. obj );
150 noLooerThread. stop ();
151 break;
152 case 3:
153 // The UI cannot be updated in a thread other than the main thread. Therefore, the message received is printed through Log.
154 Log. e (sTag, (String) msg. obj );
155 ownLooperThread. stop ();
156 break;
Default 157:
158 // The UI cannot be updated in a thread other than the main thread. Therefore, the message received is printed through Log.
159 Log. e (sTag, (String) msg. obj );
160 break;
161}
162}
163}
164 // NoLooperThread
165 class NoLooperThread extends Thread {
166 private EventHandler (mNoLooperThreadHandler;
167 public void run (){
168 loopse myloopse, mainloopse;
169 mylogoff = logoff. mylogoff ();

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.