public class ProgressBarAsyncTask extends AsyncTask<Void, Integer, String>
在继承时我们可以为AsyncTask类指定三个泛型参数,这三个参数的用途如下:
1.
new ProgressBarAsyncTask(progressBar, textView).execute();
<pre name="code" class="java" style="color: rgb(51, 51, 51); font-size: 14px; line-height: 26px;">@Override
<pre name="code" class="java" style="color: rgb(51, 51, 51); font-size: 14px; line-height: 26px;">protected String doInBackground(Void... params)
在执行AsyncTask时需要传入的参数,可用于在后台任务中使用,这边是void,所以没有参数。
2.
@Override
protected void onProgressUpdate(Integer... values)
后台任务执行时,传递该类型的参数给onProgressUpdate方法,来显示进度条
3.
@Override
protected void onPostExecute(String result)
当后台任务执行完毕后,如果需要对结果进行返回,则返回该类型参数给onPostExcute方法。
实例:
<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="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/textView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/progressBar02"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="更新progressbar" />
</LinearLayout>
package com.example.ddd;
import android.app.Activity;
import android.os.Bundle;
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 Button button;
private ProgressBar progressBar;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button03);
progressBar = (ProgressBar) findViewById(R.id.progressBar02);
textView = (TextView) findViewById(R.id.textView01);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new ProgressBarAsyncTask(progressBar, textView).execute();
}
});
}
}
package com.example.ddd;
import android.os.AsyncTask;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ProgressBarAsyncTask extends AsyncTask<Void, Integer, String> {
private ProgressBar progressBar;
private TextView textView;
public ProgressBarAsyncTask(ProgressBar progressBar, TextView textView) {
this.progressBar = progressBar;
this.textView = textView;
}
@Override
protected String doInBackground(Void... params) {
for (int i = 0; i <= 100; i += 10) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
publishProgress(i);
}
return "success";
}
@Override
protected void onPreExecute() {
textView.setText("开始");
}
@Override
protected void onProgressUpdate(Integer... values) {
int vlaue = values[0];
progressBar.setProgress(vlaue);
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
textView.setText("异步操作执行结束");
}
}