一、使用步骤
1.自定义MyAsyncTask类继承AsyncTask
2.定义AsyncTask的三种泛型参数
3.重写方法相关方法
4.调用execute方法执行异步任务
二、AsyncTask方法
1.onPreExecute()在任务执行前执行
2.doInBackground()执行任务
3.onProgressUpdate()根据doInBackground方法传递的值更新当前状态
4.onPostExecute()任务完成后业务处理
三、下载图片测试
activity_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2"
android:id="@+id/MainActivity2"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="11"
tools:srcCompat="@tools:sample/avatars" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="TextView" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="点击下载" />
</LinearLayout>
MainActivity2.java:
public class MainActivity2 extends AppCompatActivity {
static ImageView imageView;
static TextView textView;
static Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
button = findViewById(R.id.button2);
imageView = findViewById(R.id.imageView);
textView = findViewById(R.id.textView2);
textView.setText("");
button.setOnClickListener(view -> {
MyAsyncTask myAsyncTask = new MyAsyncTask(this);
myAsyncTask.execute("图片");
});
}
}
MyAsyncTask.java:
public class MyAsyncTask extends AsyncTask<String, Integer, String> {
private ProgressDialog progressDialog;
private Context context;
public MyAsyncTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(context);
progressDialog.setTitle("正在下载中,请稍后...");
//设置ProgressDialog样式为圆圈的形式
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
MainActivity2.textView.setVisibility(View.VISIBLE);
MainActivity2.textView.setText(s);
//关闭进度条
progressDialog.dismiss();
MainActivity2.imageView.setImageResource(R.drawable.ic_launcher_background);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
//跟新进度条
progressDialog.setProgress(values[0]);
}
@Override
protected String doInBackground(String... strings) {
//execute()方法传递的参数
String content = strings[0];
int flag = 0;
while (flag < 100) {
int random = new Random().nextInt(20) + 1;
flag += random;
//跟新进度,将回调onProgressUpdate()方法
publishProgress(flag);
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
return "下载" + content + "成功!";
}
}
运行截图: