Andriod提供了一个叫做AsyncTask的类,专门用于完成非UI线程,更新UI线程,她是个抽象类,需要首先子类化
AsyncTask定义了三种泛型类型 Params,Progress和Result。
Params 启动任务执行的输入参数,比如HTTP请求的URL。
Progress 后台任务执行的百分比。
Result 后台执行任务最终返回的结果,比如String。
她的几个主要方法
onPreExecute:开始执行前的准备工作
doInbackground()开始执行执行后的后台操作,在里面可以调用publishProgress()方法更新适时地任务进度
onProgressUpdate()在publishProgress()被调用后,UI线程将调用这个方法从而在页面上展示任务进度,
onPostExecute()执行完成后的操作,传送结果给UI线程
代码:
package com.example.change_ui3;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class Main extends Activity {
private TextView tv;
private ProgressBar bar;
private Button btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.tv_id);
bar=(ProgressBar) findViewById(R.id.progressBar1);
btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyAsyncTask asyncTask=new MyAsyncTask(tv, bar);
asyncTask.execute(1000);
}
});
}
}
子类化AsyncTask
package com.example.change_ui3;
import android.R.integer;
import android.os.AsyncTask;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MyAsyncTask extends AsyncTask<Integer, Integer, String> {
private TextView tv;
private ProgressBar bar;
public MyAsyncTask(TextView tv,ProgressBar bar){
this.tv=tv;
this.bar =bar;
}
protected String doInBackground(Integer... params) {
int i;
for( i=0;i<100;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
publishProgress(i);
}
return i+params[0].intValue()+"";
}
@Override
protected void onPreExecute() {
tv.setText("执行异步线程");
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
tv.setText(result);
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Integer... values) {
bar.setProgress(values[0].intValue());
}
}
XML
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main" >
<TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_id"
android:layout_alignParentRight="true"
android:layout_below="@+id/tv_id"
android:layout_marginTop="149dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/progressBar1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="106dp"
android:layout_marginLeft="22dp"
android:text="Button" />
</RelativeLayout>