By analyzing the Activity source code, we know that each Activity has a logoff, so the main thread does not need to call Logoff When receiving messages. prepare () and logoff. loop (), but the thread does not contain Looper. When the thread wants to receive messages from its own thread, it needs to call Looper. prepare () and logoff. loop (). The following example shows how to send a Message between the main thread and thread.
01 import android. app. Activity;
02 import android. OS. Bundle;
03 import android. OS. Handler;
04 import android. OS. Message;
05 import android. widget. Toast;
06
07 /**
08 * this class is the main thread. It sends a Message to the thread every two seconds. After receiving the Message, the thread sends a Message to the main thread, after receiving the Message, the main thread Toast the Message content.
09 * org. fneg. HandlerTestActivity. java
10 * Create at: 9:39:19
11 * @ author: feng
12 * Email: fengcunhan@gmail.com
13 *
14 */
15 public class HandlerTestActivity extends Activity {
16 private Handler uiHandler;
17 private threadwithlow.thread;
18 private Runnable showRunable;
19 @ Override
20 public void onCreate (Bundle savedInstanceState ){
21 super. onCreate (savedInstanceState );
22 setContentView (R. layout. main );
23 uiHandler = new Handler (){
24 @ Override
25 public void handleMessage (Message msg ){
26 switch (msg. what ){
27 case Messages. MSG_HELLO:
28 Toast. makeText (HandlerTestActivity. this, (String) msg. obj, Toast. LENGTH_SHORT). show ();
29 break;
30
31}
32}
33 };
34 thread = new ThreadWithLooper (uiHandler );
35
36 thread. start ();
37 showRunable = new Runnable (){
38
39 @ Override
40 public void run (){
41 // send a Message to the thread
42 thread. getHandler (). sendEmptyMessage (Messages. MSG_HELLO );
43 uiHandler. postDelayed (this, 2*1000 );
44}
45 };
46 uiHandler. post (showRunable );
47
48}
49
50 @ Override
51 protected void onStop (){
52 super. onStop ();
53 uiHandler. removeCallbacks (showRunable );
54}
55}
01 import android. OS. Handler;
02 import android. OS. logoff;
03 import android. OS. Message;
04
05 /**
06 * send a message from the thread to the UI thread (main thread)
07 * org. fneg. threadwithlow.java
08 * Create at: 4:58:11
09 * @ author: feng <br/>
10 * Email: fengcunhan@gmail.com
11 *
12 */
13 public class threadwithlow.extends Thread {
14 private Handler handler;
15
16 private Handler uiHandler;
17
18 public ThreadWithLooper (Handler mHandler ){
19 this. uiHandler = mHandler;
20 // initialize Handler. After receiving the Message sent by the main thread, a Message is returned to the main thread. The Message content is a string and the current time.
21 handler = new Handler (){
22
23 @ Override
24 public void handleMessage (Message msg ){
25 switch (msg. what ){
26 case Messages. MSG_HELLO:
27 Message message = new Message ();
28 message. what = Messages. MSG_HELLO;
29 message. obj = "Yes! I get a hello "+ System. currentTimeMillis ();
30 uiHandler. sendMessage (message );
31 break;
32
33}
34}
35
36 };
37}
38
39 public Handler getHandler (){
40 return handler;
41}
42
43 public void setHandler (Handler handler ){
44 this. handler = handler;
45}
46
47 @ Override
48 public void run (){
49 logoff. prepare ();
50
51 logoff. loop ();
52}
53
54
55}
01 /**
02 *
03 * org. fneg. Messages. java
04 * Create at: 4:51:20
05 * @ author: feng
06 * Email: fengcunhan@gmail.com
07 *
08 */
09 public class Messages {
10 public static final int MSG_HELLO = 0X1;
11}