AsyncTask:(实际就和handler的作用差不多)
使用AsyncTask的具体步骤:注意3个泛型必须基本数据类型对象,参数:1.在耗时操作中要传递的参数,Void 2.进度条的进度,Integer 3.当耗时操作完成后,返回的结果,String
1.新建一个类,extends AsyncTask类<Void, Integer, String>{}
2.复写其抽象方法,在异步任务(也就是doInBackground方法)执行之前,执行该方法,在主线程执行
protected void onPreExecute() {super.onPreExecute();}
3.复写其抽象方法,该方法在后台执行(可以理解为子线程),可以做耗时的工作
protected Result doInBackground(Params... params){return null;}可以接收到doInBackground返回数据
4.复写其方法抽象方法,在异步任务(也就是doInBackground方法)执行之后,执行该方法,在主线程执行
protected void onPostExecute(Void result) {super.onPostExecute(result);}
protected void onPreExecute() {super.onPreExecute();}
3.复写其抽象方法,该方法在后台执行(可以理解为子线程),可以做耗时的工作
protected Result doInBackground(Params... params){return null;}可以接收到doInBackground返回数据
4.复写其方法抽象方法,在异步任务(也就是doInBackground方法)执行之后,执行该方法,在主线程执行
protected void onPostExecute(Void result) {super.onPostExecute(result);}
5.复写其抽象方法,用来跟新你的UI的,publishProgress被调用的时候,以下方法被执行
protected void onProgressUpdate(Integer... values) {super.onProgressUpdate(values);}
6.开启一个异步任务,在主线程里新建继承了AsyncTask自定义的类,调用execute()方法.
new myAsyncTasK().execute();//execute()方法一定要用,相当于开启线程的start。
new myAsyncTasK().execute();//execute()方法一定要用,相当于开启线程的start。
用AsyncTask实现的、案例效果图:
在onPreExecute方法中设置了弹出的提示框的信息;
在doInBackground方法中进行了网络请求,成功获取图片数据,因为读取的是字节,把数据转换为字节,要把图片放在imagerVIew控件上,需要把字节转换为bitmap位图
;
在onProgressUpdate方法中设置了进度条的显示;
在onPostExecute方法中设置了结束后的操作,更新UI;
在按钮的监听开启了异步任务;
MainActivity代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn;
private ImageView imageView;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
progressDialog = new ProgressDialog(this);
btn = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.imageView);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//异步任务开启
new MyAsyTask().execute();
}
//参数:1.在耗时操作中要传递的参数,Void 2.进度条的进度,Integer 3.当耗时操作完成后,返回的结果,String
public class MyAsyTask extends AsyncTask<Void,Integer,Bitmap>{
private Bitmap bitmap;
@Override
//在doInBackground执行之前将会被调用
protected void onPreExecute() {
//设置标题
progressDialog.setTitle("提示");
//设置信息
progressDialog.setMessage("正在登陆");
//设置提示框样式
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置提示框点击其他区域不消失
progressDialog.setCancelable(false);
//显示提示框
progressDialog.show();
}
@Override
//所有耗时操作都在此方法中完成
protected Bitmap doInBackground(Void... params) {
try {
URL url = new URL("http://imgsrc.baidu.com/forum/pic/item/20ee6c81800a19d8b5db1e533afa828ba71e4659.jpg");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
//获取响应状态码,如果为200,响应成功,否则请求失败
if(httpURLConnection.getResponseCode()==200){
//从httpURLConnection连接对象中,获取数据的输入流
InputStream inputStream = httpURLConnection.getInputStream();
//字节数据输出流,用来存储数据的(inputStream中的数据)
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//获取图片的总长度
int contentLength = httpURLConnection.getContentLength();
byte[] bytes = new byte[1024];
int len;
int total_length=0;
int value=0;
//如果每次读取的字节长度不等于-1,也就是说没有读取完成,那么下次接着读取
while (-1!=(len = inputStream.read(bytes))){
//获取当前的百分比进度(用当前读取的长度/总长度*100)
value = (int) (total_length/(double)contentLength*100);
//更新现在读取的长度
total_length = total_length+len;
//参数: 1.读取的字节对象 2.从数组哪里开始 3.每次存入多少的长度
outputStream.write(bytes,0,len);
//传递百分比
publishProgress(value);
//刷新,强制释放缓存区
outputStream.flush();
}
//把字节数组输出流转换为字节数组
byte[] bytes1 = outputStream.toByteArray();
//把字节数组转换为bitmap位图
bitmap = BitmapFactory.decodeByteArray(bytes1,0,contentLength);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
//用来跟新你的UI的,publishProgress被调用的时候,以下方法被执行
protected void onProgressUpdate(Integer... values) {
//values的值就是进度
progressDialog.setProgress(values[0]);
}
@Override
//当耗时操作结束的时候调用此方法,更新UI
protected void onPostExecute(Bitmap bitmap) {
//关闭对话框
progressDialog.setProgress(0);
//解除对话框
progressDialog.dismiss();
imageView.setImageBitmap(bitmap);
}
}
}