package com.zsh.progressbar;
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 {
private ProgressBar progressBar ;
private TextView textView ;
private Button button;
public MyHandler myHandler ;
class MyHandler extends Handler{
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);
// 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;
//查看当前操作的线程名称,与前面的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文件---------
<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线程中进行的。由于代码比较简单,就没有注释说明!