Android子线程中更新UI的三种方法和获取message的两种方法总结

来源:互联网 发布:northwind数据库下载 编辑:程序博客网 时间:2024/04/30 12:05

       Android主线程和子线程交互时,子线程中更新UI的三种方法和获取message的两种方法总结一下;主线程和子线程交互时,如果在主线程更新UI相信大家都知道1.Handler  2.AsyncTask。



[html] view plaincopy
  1. <span style="font-size:14px;">public class MainActivity extends Activity implements OnClickListener {  
  2.   
  3.     private Button button1, button2, button3, button4, button5;  
  4.     private TextView textView1, textView2, textView3, textView4, textView5;  
  5.   
  6.     private Handler mHandler;  
  7.     private SubThread5 handlerThread;  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.         button1 = (Button) findViewById(R.id.button1);  
  14.         button2 = (Button) findViewById(R.id.button2);  
  15.         button3 = (Button) findViewById(R.id.button3);  
  16.         button4 = (Button) findViewById(R.id.button4);  
  17.         button5 = (Button) findViewById(R.id.button5);  
  18.         textView1 = (TextView) findViewById(R.id.textView1);  
  19.         textView2 = (TextView) findViewById(R.id.textView2);  
  20.         textView3 = (TextView) findViewById(R.id.textView3);  
  21.         textView4 = (TextView) findViewById(R.id.textView4);  
  22.         textView5 = (TextView) findViewById(R.id.textView5);  
  23.         button1.setOnClickListener(this);  
  24.         button2.setOnClickListener(this);  
  25.         button3.setOnClickListener(this);  
  26.         button4.setOnClickListener(this);  
  27.         button5.setOnClickListener(this);  
  28.         new SubThread4().start();  
  29.         getWindow().setBackgroundDrawable(null);  
  30.   
  31.     }  
  32.   
  33.     class SubThread1 extends Thread {  
  34.   
  35.         public void run() {  
  36.             MainActivity.this.runOnUiThread(new Runnable() {  
  37.   
  38.                 @Override  
  39.                 public void run() {  
  40.                     // 在这里执行你要想的操作 比如直接在这里更新ui或者调用回调在 在回调中更新ui  
  41.                     textView1.setText("SubThread1");  
  42.                 }  
  43.             });  
  44.         }  
  45.     }  
  46.   
  47.     class SubThread2 extends Thread {  
  48.   
  49.         public void run() {  
  50.             new Handler(MainActivity.this.getMainLooper()).post(new Runnable() {  
  51.   
  52.                 @Override  
  53.                 public void run() {  
  54.                     // 在这里执行你要想的操作 比如直接在这里更新ui或者调用回调在 在回调中更新ui  
  55.                     textView2.setText("SubThread2");  
  56.                 }  
  57.             });  
  58.         }  
  59.     }  
  60.   
  61.     class SubThread3 extends Thread {  
  62.   
  63.         public void run() {  
  64.             textView3.post(new Runnable() {  
  65.   
  66.                 @Override  
  67.                 public void run() {  
  68.                     // 在这里执行你要想的操作 比如直接在这里更新ui或者调用回调在 在回调中更新ui  
  69.                     textView3.setText("SubThread3");  
  70.                 }  
  71.             });  
  72.         }  
  73.     }  
  74.   
  75.     class SubThread4 extends Thread {  
  76.   
  77.         public void run() {   
  78.             Looper.prepare();// 1、初始化Looper  
  79.             mHandler = new Handler() {// 2、绑定handler到CustomThread实例的Looper对象  
  80.                 public void handleMessage(Message msg) {// 3、定义处理消息的方法  
  81.                     switch (msg.what) {  
  82.                     case 0:  
  83.                         // 这里只是能通信 但是不能在子线程改变UI,如果改变textView4的值将会报错(Only the original thread that created a view hierarchy can touch its views)  
  84.                         //textView4.setText("SubThread4");  
  85.                         System.out.println("SubThread4");  
  86.                     }  
  87.                 }  
  88.             };  
  89.             Looper.loop();// 4、启动消息循环}  
  90.         }  
  91.     }  
  92.   
  93.     private class SubThread5 extends HandlerThread implements Callback {  
  94.   
  95.         public SubThread5(String name) {  
  96.             super(name);  
  97.         }  
  98.   
  99.         @Override  
  100.         public boolean handleMessage(Message msg) {  
  101.             // 这里只是能通信 但是不能在子线程改变UI,如果改变textView4的值将会报错(Only the original thread that created a view hierarchy can touch its views)  
  102.             // textView5.setText("SubThread5");  
  103.             System.out.println(" handleMessage CurrentThread = "  
  104.                     + Thread.currentThread().getName());  
  105.             return true;  
  106.         }  
  107.     }  
  108.   
  109.     @Override  
  110.     public void onClick(View v) {  
  111.         // TODO Auto-generated method stub  
  112.         switch (v.getId()) {  
  113.         case R.id.button1:  
  114.             new SubThread1().start();  
  115.             break;  
  116.         case R.id.button2:  
  117.             new SubThread2().start();  
  118.             break;  
  119.         case R.id.button3:  
  120.             new SubThread3().start();  
  121.             break;  
  122.         case R.id.button4:  
  123.             mHandler.sendEmptyMessage(0);  
  124.             break;  
  125.         case R.id.button5:  
  126.             handlerThread = new SubThread5("myHanler");  
  127.             handlerThread.start();  
  128.             // 注意:  
  129.             // 这里必须用到handler的这个构造器,因为需要把callback传进去,从而使自己的HandlerThread的handlerMessage来替换掉Handler原生的handlerThread  
  130.             mHandler = new Handler(handlerThread.getLooper(), handlerThread);  
  131.             mHandler.sendEmptyMessage(0);  
  132.             break;  
  133.         }  
  134.     }  
  135. }</span>  


注:1.Handler 与谁相关联不是看声明在什么地方,是看与哪个线程的looper挂钩。默认是主线程的looper.因为主线程中默认就有了looper,消息循环队列。

2. public final void runOnUiThread (Runnable action)

Added in API level 1

Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

如果当前线程是UI主线程,则直接执行Runnable的代码;否则将Runnable post到UI线程的消息队列。

Parameters
actionthe action to run on the UI thread

Activity.java中runOnUiThread源码

[java] view plaincopy
  1. public final void runOnUiThread(Runnable action) {  
  2.        if (Thread.currentThread() != mUiThread) {  
  3.            mHandler.post(action);//将Runnable Post到消息队列,由内部的mHandler来处理,实际上也是Handler的处理方式  
  4.        } else {  
  5.            action.run();//已经在UI线程,直接运行。  
  6.        }  
  7.    } 
0 0
原创粉丝点击