Android非UI线程更新UI的几种方法

1.handler.send(Message message),或者handler.post (Runnable r);

2.View.post(Runnable r);

 

  
  
  
  
  1. /** 
  2.  * 更新UI方法之 view.post(Runnable r) 
  3.  * @author vincentTung 
  4.  * 
  5.  */ 
  6. class  ViewPostThread extends Thread{ 
  7.  
  8.         @Override 
  9.         public void run() { 
  10.             super.run(); 
  11.             while(count<101){ 
  12.                 /** 
  13.                  * view.post(Runnable) 
  14.                  */ 
  15.                 bar.post(new Runnable() { 
  16.  
  17.                     @Override 
  18.                     public void run() { 
  19.                         isChanging = true
  20.                         bar.setProgress(count); 
  21.                         count++; 
  22.  
  23.                     }}); 
  24.                  
  25.                 /** 
  26.                  * view.postDelayed(Runnable,delayTime) 
  27.                  */ 
  28. //              bar.postDelayed(new Runnable() { 
  29. // 
  30. //                  @Override 
  31. //                  public void run() { 
  32. //                      isChanging = true
  33. //                      bar.setProgress(count); 
  34. //                      count++; 
  35. // 
  36. //                  }},100); 
  37. //               
  38.                 try { 
  39.                     Thread.sleep(100); 
  40.                 } catch (InterruptedException e) { 
  41.                     e.printStackTrace(); 
  42.                 } 
  43.             } 
  44.  
  45.         } 
  46.     } 

3.Activity的RunOnUIThread(Runnable r);

   
   
   
   
  1. class  ViewPostThread extends Thread{ 
  2.  
  3.         @Override 
  4.         public void run() { 
  5.             super.run(); 
  6.             while(count<101){ 
  7.                 runOnUiThread(new Runnable() { 
  8.  
  9.                     @Override 
  10.                     public void run() { 
  11.                         isChanging = true
  12.                         bar.setProgress(count); 
  13.                         count++; 
  14.  
  15.                     }}); 
  16.                  
  17.                 try { 
  18.                     Thread.sleep(100); 
  19.                 } catch (InterruptedException e) { 
  20.                     e.printStackTrace(); 
  21.                 } 
  22.             } 
  23.  
  24.         } 
  25.     } 

4.通过HandlerThread进行更新

 

    
    
    
    
  1. /** 
  2.  *  
  3.  *  
  4.  * 更新UI方法之  
  5.  * ----  HandlerThread 
  6.  *  
  7.  * HandlerThread与普通Thread的区别是,普通Thread 默认不带Looper的, 
  8.  * 需要调用Looper.prepare()方法为线程分配出有一个Looper,然后通过Looper.loop()方法让Looper转起来,循环分发消息Message 
  9.  * 而HandlerThread就可以看作是默认就非配有Looper的线程Thread 
  10.  * 1.在子线程中声明Handler  
  11.  * 2.在子线程的run()方法中:Looper.prepare();//为子线程分配一个Looper 
  12.  * 3.在子线程的run()方法中:处理操作,并发送Message 
  13.  * 4.在子线程的run()方法中: Looper.loop();//让Loop转起来 
  14.  * 
  15.  * @author vincentTung 
  16.  * 
  17.  */ 
  18.  
  19.     private HandlerThread handlerThread =new HandlerThread("handler_thread"){ 
  20.  
  21.         private Handler handler = new Handler(){ 
  22.  
  23.             public void handleMessage(android.os.Message msg) { 
  24.                 count++; 
  25.                 bar.setProgress(count); 
  26.                 if(count==100){ 
  27.                      
  28.                     Toast.makeText(HandlerThreadTest.this, "Done", 1).show(); 
  29.                 } 
  30.             }; 
  31.         }; 

使用时候调用

handlerThread.start();

5.在子线程中更新,前提是得Loop.prepare( );

 

     
     
     
     
  1. /** 
  2.  *  
  3.  *  
  4.  * 更新UI方法之  
  5.  * ----  带Looper的子线程 
  6.  * 1.在子线程中声明Handler  
  7.  * 2.在子线程的run()方法中:Looper.prepare();//为子线程分配一个Looper 
  8.  * 3.在子线程的run()方法中:处理操作,并发送Message 
  9.  * 4.在子线程的run()方法中: Looper.loop();//让Loop转起来 
  10.  * 
  11.  * @author vincentTung 
  12.  * 
  13.  */ 
  14. class  NormalThread extends Thread{ 
  15.         private int count=0
  16.         private Handler handler = new Handler(){ 
  17.             public void handleMessage(android.os.Message msg) { 
  18.                 count++; 
  19.                 bar.setProgress(count); 
  20.             };}; 
  21.             @Override 
  22.             public void run() { 
  23.                 super.run(); 
  24.                 Looper.prepare(); 
  25.                 while(count<=100){ 
  26.  
  27.                     handler.sendEmptyMessage(0); 
  28.                     try { 
  29.                         Thread.sleep(100); 
  30.                     } catch (InterruptedException e) { 
  31.                         e.printStackTrace(); 
  32.                     } 
  33.  
  34.  
  35.                 } 
  36.                 Looper.loop(); 
  37.             } 
  38.  
  39.  
  40.     } 

你可能感兴趣的:(android,更新UI,UI线程)