Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 實例講解Android多線程應用開發中Handler的使用

實例講解Android多線程應用開發中Handler的使用

編輯:關於Android編程

其實可以理解Handler為主線程和另外的線程之間進行數據更新的東東,並且Handler在主線程中,並在Handler直接調用線程的run方法

package com.Handler02; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
 
public class Handler02Activity extends Activity { 
  /** Called when the activity is first created. */ 
  private Handler handler=new Handler(); 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    handler.post(thread1); 
    setContentView(R.layout.main); 
    
    System.out.println("================Main==============="+Thread.currentThread().getId()); 
    System.out.println("============Main========="+Thread.currentThread().getName()); 
  } 
  Runnable thread1=new Runnable() { 
     
    @Override 
    public void run() { 
      System.out.println("======thread1==============="+Thread.currentThread().getId()); 
      System.out.println("========thread1============"+Thread.currentThread().getName()); 
      try { 
        Thread.sleep(10000); 
      } catch (Exception e) { 
        // TODO: handle exception 
      } 
       
    } 
  }; 
} 

結果是主線程等待10s後才顯示出來,並且線程ID

package com.Handler4; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.View; 
import android.widget.TextView; 
 
public class Handler4Activity extends Activity { 
  /** Called when the activity is first created. */ 
  private TextView textView; 
  private MyHandler myHandler=new MyHandler(); 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    textView=(TextView)this.findViewById(R.id.textView1); 
  } 
  class MyHandler extends Handler{ 
    //從消息隊列中取出並handleMessage處理消息 
    @Override 
    public void handleMessage(Message msg) { 
      textView.setText((String)msg.obj); 
    } 
  } 
  public void download(View view){ 
    Thread thread=new Thread(new Runnable() { 
       
      @Override 
      public void run() { 
        try { 
          String s="zhangzhao"; 
          Thread.sleep(5000); 
          //數據發送出來 
          //textView.setText(s); 
          Message message=Message.obtain();//最好不要new,這個obtain會節省資源 
          message.obj=s; 
          myHandler.sendMessage(message);//沒有指定looper那麼就會發送給主線程中的looper 
           
        } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
        } 
         
      } 
       
    }); 
    thread.start();//會出錯原因是在非UI線程裡面直接操作UI 
    //主線程維護了一個消息對列,當其他的線程有數據需要傳遞給主線程的時候,你就把數據封裝成一個Message對象,然後放在主線程的 
    //消息隊列中去,對應消息的處理由Looper,子線程通過Handler把下載完的數據封裝到Message裡面,然後把消息取出來交給Handler進行處理 
    //通過Handle和Message實現兩個線程之間達到共享數據 
  } 
} 

package com.Handler5; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
 
public class Handler5Activity extends Activity { 
  /** Called when the activity is first created. */ 
  private ProgressBar progressBar; 
  private Button button; 
  private TextView textView; 
  private MyHandler myHandler=new MyHandler(); 
  int i=0; 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
   
    setContentView(R.layout.main); 
    progressBar = (ProgressBar)this.findViewById(R.id.progressBar1); 
    textView=(TextView)this.findViewById(R.id.textView1); 
  } 
  class MyHandler extends Handler{ 
    @Override 
    public void handleMessage(Message msg) { 
      int pos =(Integer)msg.obj; 
      progressBar.setProgress(pos); 
      textView.setText(pos+"%"); 
       
    } 
  } 
  public void downLoad(View view){ 
      
    new Thread(new Runnable() { 
       
      @Override 
      public void run() { 
        while(i<=100){ 
          try { 
            Thread.sleep(300); 
            i+=10; 
            Message message=Message.obtain(); 
            message.obj=i; 
            myHandler.sendMessage(message); 
          } catch (InterruptedException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
          } 
         
        } 
         
      } 
    }).start(); 
  } 
} 

效果:

2016131175347004.jpg (283×189)

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved