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线程中进行的。因为代码比较简单,就没有注释说明!

相关文章
相关标签/搜索