AsyncTask< 地址类型Params, 进度值类型Progress, 结果类型Result> { //String、URL, Integer, Bitmap、void
onPreExecute(){ //运行在主线程MainThread,准备工作 }
totalLen = connection.getContentLength();
protect Result doInBackground(Params... params) { //运行在子线程WorkThread,耗时操作 return result;}
int progress = (int) (curLen*100/(double)totalLen);
publishProgress(progress);
onPostExcute(Result result) { //在主线程中调用,处理结果 }
BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 把一个字节数组变成Bitmap对象
onProgressUpdate(Progress... progress) { //在publishProgress()调用之后在主线程调用,用来显示进度值 }
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("...");
dialog.setProgressStyled(ProgressDialog.STYLE_HORIZONTAL);
dialog.setIcon(...);
dialog.setMessage("Loading...");
dialog.setCanceledOnTouchOutside(false);
dialog.show();
dialog.setProgress(int value);
dialog.setOnDismissListener(
onDismiss(DialogInterface dialog) { //myAsync.cancel(true); }
)
dialog.dismiss();
onCanceled(Result result) { //取消异步任务之后再主线程中运行}
}
主线程:new MyAsyncTask.excute(path);//一个MyAsyncTask对象只能调用excute()一次