AsyncTask
@Override
protected Void doInBackground(Void... params)
{
//必须重写,异步执行后台线程将要完成的任务
Log.i("tag", "doInBackground");
publishProgress();
return null;
}
@Override
protected void onPreExecute()
{
//执行后台耗时操作前被调用,通常完成一些用户初始化操作
super.onPreExecute();
Log.i("tag", "onPreExecute");
}
@Override
protected void onPostExecute(Void aVoid)
{
//当doInBackground()完成后,系统自动调用onPostExecute()方法,并将doInBackground()方法返回的数值传给该方法
super.onPostExecute(aVoid);
Log.i("tag", "onPostExecute");
}
@Override
protected void onProgressUpdate(Void... values)
{
//在doInBackground()方法中调用publishProgress()方法,更新任务的执行进度后,就会触发该方法,返回任务进度
super.onProgressUpdate(values);
Log.i("tag", "onProgressUpdate");
}
利用AsyncTask通过网络加载图片
public class ImgAsyncTask extends AsyncTask<String, Void, Bitmap>//<图片的URL地址,不返回进度,结果返回图片Bitmap>
{
//Params 需要传进来的值 Prigress 返回的进度 Result 返回的结果
private ProgressBar progressBar;
private ImageView imageView;
public ImgAsyncTask(ProgressBar progressBar, ImageView imageView)
{
this.progressBar = progressBar;
this.imageView = imageView;
}
@Override
protected Bitmap doInBackground(String... params)//(String... params)可变长度数组
{
String imgURL = params[0];//因为只传进来一个值,所以params数组在0取值
Bitmap bitmap = null;
URLConnection urlConnection;
InputStream inputStream;
try
{
urlConnection = new URL(imgURL).openConnection();
inputStream = urlConnection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
//通过decodeStream方法解析输入流,转化成Bitmap
Thread.sleep(2000);
bitmap = BitmapFactory.decodeStream(bufferedInputStream);
inputStream.close();
bufferedInputStream.close();
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
//将bitmap作为返回值
return bitmap;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
imageView.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bitmap bitmap)
{
super.onPostExecute(bitmap);
progressBar.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
imageView.setImageBitmap(bitmap);
}
@Override
protected void onProgressUpdate(Void... values)
{
super.onProgressUpdate(values);
}
}
在MainActivity里 调用new ImgAsyncTask(progressBar,imageView).execute(imgURL);启动异步加载
publishProgress(int i);在doInBackground设置Progress进度返回给onProgressUpdate
@Override
protected void onProgressUpdate(Integer... values)
{
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
}
在onProgressUpdate设置Progress进度,返回给UI
销毁AsyncTask
//getStatus获取当前状态
if (progressAsyncTask.getStatus() == AsyncTask.Status.RUNNING)
{
//cancel撤销 只是将对应的AsyncTask标记为cancel状态,UI上暂停但是线程并未停止。
//java无法强制停止线程
progressAsyncTask.cancel(true);
}
如果想要中断线程,可以在AsyncTask中使用if(isCancelled())判断cancel是否为真,如果为真break破坏循环
@Override
protected Void doInBackground(Void… params)
{
Looper.prepare();
for (int i = 0; i < mediaPlayer.getDuration(); i++)
{
publishProgress(mediaPlayer.getCurrentPosition());
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
Looper.prepare();
return null;
}