Android多線程更新ProgressBar(Handler、HandlerThread)

package com.zsh.progressbar; android

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView; 多線程

public class MainActivity extends Activity { app

 private ProgressBar progressBar ;
 private TextView textView ;
 private Button button;
 public MyHandler myHandler ; oop

 class MyHandler extends Handler{ post


  public MyHandler(){
  }
  
  public MyHandler(Looper looper){
   super(looper);
  }
  
  public void handleMessage(Message msg){
   System.out.println("handler------->"+Thread.currentThread().getId());
   if(msg.arg1 <= 100){
    System.out.println("------>  "+msg.arg1);
    Log.i("更新進度---->", String.valueOf(msg.arg1));
    progressBar.setProgress(msg.arg1);
    myHandler.post(updateProgress);
   }
  }
 }
 
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  progressBar = (ProgressBar) this.findViewById(R.id.progressBar1);
  button  = (Button)this.findViewById(R.id.button1); this


  // UI線程名
  System.out.println("main------->"+Thread.currentThread().getId());
  
  button.setOnClickListener(new OnClickListener(){
   public void onClick(View v) {
    // 建立一個包含Looper的線程,這裏若是沒有HandlerThread的調用,會直接將後邊的MyRunnable放到UI線程隊列(myHandler.post(new MyRunnable())) 
    HandlerThread handlerThread = new HandlerThread("handler_thread"); 
    handlerThread.start();      // 啓動自定義處理線程 
          
    myHandler = new MyHandler(handlerThread.getLooper());       // 將handler綁定到新線程   
    progressBar.setVisibility(View.VISIBLE);
    myHandler.post(updateProgress);
   }
  });
 }
 
 Runnable updateProgress = new Runnable(){
  int i = 0;
  public void run() {
   i+=10;
   Message msg = myHandler.obtainMessage();
   msg.arg1 = i; spa

//查看當前操做的線程名稱,與前面的UI線程名應該不相同,證實此操做是多線程
   System.out.println("runnable------->"+Thread.currentThread().getId());
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   
   msg.sendToTarget();
   if(i == 100){
    myHandler.removeCallbacks(updateProgress);
   }
  }
 };
} 線程

 

--------  layout文件--------- xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" > 隊列

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="198dp"
        android:layout_height="wrap_content"
        android:text="@string/def"
        android:textIsSelectable="true" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_name" />

</LinearLayout>

 

------------------------------------------------

在網上查看了很多資料,不少人轉載的文章根本就不是多線程,更新ProgressBar的操做是在UI線程中進行的。因爲代碼比較簡單,就沒有註釋說明!

相關文章
相關標籤/搜索