您的位置:首页 > 移动开发 > Android开发

Android中AsyncTask解析

2016-01-31 10:05 495 查看
/* A.异步任务类的总结

* 一. 泛型<Params, Progress, Result>

* 1.Params访问资源的URL;

*

* 从new MyAsyncTask().execute(params);

* 传到doInBackground(String... params)

*

* 2.Progress下载进度

*

* 从publishProgress(downloadlenth * 100 / contentlenth);

* 传到onProgressUpdate(Integer... values)

*

* 3.Result doInBackground完成的结果

*

* 从return bitmap;

* 传到onPostExecute(Bitmap result)

* 以上三个参数都要在定义好异步任务类时定义好,下面方法的参数会根据他们改变

* 二、方法

* onPreExecute()

* 此方法是主线程,在执行之前,通常在里面做一些准备操作

*

* doInBackground(String... params)

* 此方法是子线程,在此方法中完成下载

*

* onProgressUpdate(Integer... values)

* 此方法是主线程,更新UI控件

*

* onPostExecute(Bitmap result)

* 此方法是主线程,通过参数拿到 doInBackground的返回值

*

* B.控件ProgressDialog进度对话框,很好用

* 凡是Dialog控件都不用再xml文件中写

* 都是用代码实现的

*

*/

public class MainActivity extends Activity {

private ImageView iv;

// 文件总大小

private int contentlenth;

// 文件下载量

private int downloadlenth;

private Bitmap bitmap;

// 定义进度对话框

private ProgressDialog dialog;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

init();

}

public void btn_asynctask(View v) {

// 主线程

String params = "http://10.0.2.2:7070/FileDownLoad/car.jpg";

// 启动异步任务类并通过参数把路径传到doInBackground

new MyAsyncTask().execute(params);

}

class MyAsyncTask extends AsyncTask<String, Integer, Bitmap> {

// 此方法是主线程,在执行之前,通常在里面做一些准备操作

@Override

protected void onPreExecute() {

super.onPreExecute();

dialog.show();

}

// 此方法是子线程,在此方法中完成下载

// 参数String... params,相当于String[]params;

@Override

protected Bitmap doInBackground(String... params) {

ByteArrayOutputStream bos = null;

InputStream is = null;

// 完成下载

try {

String path = params[0];

URL url = new URL(path);

HttpURLConnection httpurlconnetion = (HttpURLConnection) url

.openConnection();

httpurlconnetion.setConnectTimeout(3000);

httpurlconnetion.setRequestMethod("GET");

if (httpurlconnetion.getResponseCode() == 200) {

System.out.println("网络连接成功。。。");

is = httpurlconnetion.getInputStream();

contentlenth = httpurlconnetion.getContentLength();

bos = new ByteArrayOutputStream();

int len = 0;

byte[] bs = new byte[1024];

while ((len = is.read(bs)) != -1) {

bos.write(bs, 0, len);

downloadlenth += len;

// 发送进度

publishProgress(downloadlenth * 100 / contentlenth);

}

byte[] content = bos.toByteArray();

bitmap = BitmapFactory.decodeByteArray(content, 0,

content.length);

bos.close();

is.close();

}

} catch (Exception e) {

e.printStackTrace();

}

return bitmap;

}

// 此方法是主线程,更新UI控件

// Integer... values参数相当于Integer[]values

@Override

protected void onProgressUpdate(Integer... values) {

super.onProgressUpdate(values);

// 设置进度条

dialog.setProgress(values[0]);

}

// 此方法是主线程,通过参数拿到 doInBackground的返回值

@Override

protected void onPostExecute(Bitmap result) {

super.onPostExecute(result);

// 把位图展现到界面上

iv.setImageBitmap(result);

dialog.dismiss();

}

}

private void init() {

iv = (ImageView) findViewById(R.id.iv);

// 实例化进度对话框,并进行相关设置

dialog = new ProgressDialog(this);

// 给对话框中设置进度条

dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: