AsyncTask<Params, Progress, Result>
是一个抽象类,通常用于被继承,继承AsyncTask需要指定如下三个泛型参数:
Params:启动任务时输入参数的类型
Progress:后台任务执行中返回进度值的类型
Result:后台执行任务完成后返回结果的类型
AsyncTask子类的回调方法:
doInBackground:必须重写,异步执行后台线程将要完成的任务
onPreExecute:执行后台耗时操作前被调用,通常用户完成一些初始化操作
onPostExecute:当doInBackground()完成后系统会自动调用该方法,并将doInBackground方法返回的值传给该方法
onProgressUpdate:在doInBackground()方法中调用publishProgress()方法更新任务的执行进度后,就会触发该方法
二、执行顺序
三、案例
package com.example.asynctasktest;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
public ImageView iv;
private ProgressBar bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
iv.setVisibility(View.GONE);
bar = (ProgressBar) findViewById(R.id.progress);
LoadImg img = new LoadImg();
img.execute("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png");
}
class LoadImg extends AsyncTask<String, Integer, Bitmap> {
private InputStream inputStream;
private Bitmap bitmap;
private BufferedInputStream in;
@Override
protected Bitmap doInBackground(String... params) {
try {
URL url = new URL(params[0]);
URLConnection c = url.openConnection();
inputStream = c.getInputStream();
in = new BufferedInputStream(inputStream);
Thread.sleep(3000);
bitmap = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bitmap;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
System.out.println("onPreExecute");
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
System.out.println("onProgressUpdate");
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (result != null) {
iv.setVisibility(View.VISIBLE);
iv.setImageBitmap(result);
bar.setVisibility(View.GONE);
} else {
iv.setImageResource(R.drawable.ic_launcher);
}
System.out.println("onPostExecute");
}
}
}
2、<FrameLayout 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"
tools:context=".MainActivity" >
<ProgressBar
android:layout_gravity="center"
android:id="@+id/progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_gravity="center"
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</FrameLayout>