简单点说,哪个线程new Handler()了当前这个对象,handerl就会处理哪个线程。
所以,android UI线程里创建了一个Handler,再用次handler来更改UI,就会没有问题额。
1 package com.example.fd; 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.Menu; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 import android.widget.ImageButton; 12 import android.widget.TextView; 13 14 import com.example.fd.R.id; 15 16 public class MainActivity extends Activity { 17 Handler mh; 18 TextView te; 19 Button bt1; 20 ImageButton bt2; 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 te=(TextView)findViewById(id.textView1); 26 bt2=(ImageButton)findViewById(id.imageButton1); 27 bt1=(Button)findViewById(id.button1); 28 bt2.setOnClickListener(new OnClickListener(){ 29 public void onClick(View v){ 30 Runnable rn=new Runnable(){ 31 public void run(){ 32 te.setText("aaaaa"); 33 } 34 }; 35 Message ms=new Message(); 36 ms.what=1; 37 mh.sendMessage(ms); 38 } 39 }); 40 bt1.setOnClickListener(new OnClickListener(){ 41 public void onClick(View v){ 42 Runnable rn=new Runnable(){ 43 public void run(){ 44 te.setText("This is now Handler"); 45 } 46 }; 47 Message msg=new Message(); 48 msg.what=2; 49 //mh.post(rn); 50 mh.sendMessage(msg); 51 } 52 }); 53 mh=new Handler(){ 54 public void handleMessage(Message msg){ 55 switch (msg.what){ 56 case 1: 57 te.setText("this is from Handler"); 58 break; 59 case 2: 60 te.setText("This is from Hander 2"); 61 break; 62 63 } 64 } 65 }; 66 67 } 68 69 70 @Override 71 public boolean onCreateOptionsMenu(Menu menu) { 72 // Inflate the menu; this adds items to the action bar if it is present. 73 getMenuInflater().inflate(R.menu.main, menu); 74 return true; 75 } 76 77 }
package com.example.fd; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageButton; import android.widget.TextView; import com.example.fd.R.id; public class MainActivity extends Activity { Handler mh; TextView te; Button bt1; ImageButton bt2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); te=(TextView)findViewById(id.textView1); bt2=(ImageButton)findViewById(id.imageButton1); bt1=(Button)findViewById(id.button1); bt2.setOnClickListener(new OnClickListener(){ public void onClick(View v){ Runnable rn=new Runnable(){ public void run(){ te.setText("aaaaa"); } }; Message ms=new Message(); ms.what=1; mh.sendMessage(ms); } }); bt1.setOnClickListener(new OnClickListener(){ public void onClick(View v){ Runnable rn=new Runnable(){ public void run(){ te.setText("This is now Handler"); } }; Message msg=new Message(); msg.what=2; //mh.post(rn); mh.sendMessage(msg); } }); mh=new Handler(){ public void handleMessage(Message msg){ switch (msg.what){ case 1: te.setText("this is from Handler"); break; case 2: te.setText("This is from Hander 2"); break; } } }; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }