首页 > 学院 > 开发设计 > 正文

基于AsyncTask的带进度条的下载功能Demo实现

2019-11-06 09:48:59
字体:
来源:转载
供稿:网友

Android中常用的异步大概有两种方式:Handler异步消息机制和AsyncTask异步类。后者是前者的封装,是一种轻量级的异步处理方式,适合简单的异步处理。

AsyncTask是一个抽象类,需要实现4个常用的方法,如下:

(1) doInBackground: 子线程中运行,耗时操作。将执行结束的结果返回onPostExecute()参数中

(2) onPReExecute: 运行在UI线程中,任务执行前的准备,比如弹出进度条

(3) onPostExecute:  运行在UI线程中,处理异步线程的任务结果

(4) onProgressUpdate:  运行在UI线程中,更新当前进度条信息,被publishProgress回调

代码如下:

MainActivity.java

package com.steven.myasynctask;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {    private DownloadTask task;    private Button button;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.btn_start);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                task = new DownloadTask(MainActivity.this);                //该task只能被执行一次,否则多次调用时将会出现异常                task.execute();            }        });    }}

DownloadTask.java

package com.steven.myasynctask;import android.app.Activity;import android.content.Context;import android.os.AsyncTask;import android.view.View;import android.widget.ProgressBar;import android.widget.TextView;import android.widget.Toast;/* AsyncTask<Params, Progress, Result>        Params 启动任务执行的输入参数,比如HTTP请求的URL。        Progress 后台任务执行的进度。        Result 后台执行任务最终返回的结果,比如String。*/public class DownloadTask extends AsyncTask<Void, Integer, Boolean> {    ProgressBar pb;    TextView tvProgress;    int progress = 0;    protected Context context;    public DownloadTask(Activity activity) {        context = activity;        pb = (ProgressBar) activity.findViewById(R.id.progress_bar);        tvProgress = (TextView) activity.findViewById(R.id.tv_progress);    }    //子线程中运行,耗时操作。将执行结束的结果返回onPostExecute()参数中    @Override    protected Boolean doInBackground(Void... params) {        while (true) {            //更新进度信息,回调onProgressUpdate函数            publishProgress(progress);            if (progress != 100) {                progress += 10;                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    e.printStackTrace();                }            } else {                return true;            }        }    }    //运行在UI线程中,任务执行前的准备,比如弹出进度条    @Override    protected void onPreExecute() {        super.onPreExecute();        pb.setVisibility(View.VISIBLE);    }    //运行在UI线程中,执行处理返回的结果    @Override    protected void onPostExecute(Boolean aBoolean) {        super.onPostExecute(aBoolean);        if (aBoolean) {            Toast.makeText(context, "下载完毕", Toast.LENGTH_SHORT).show();            pb.setVisibility(View.INVISIBLE);        }    }    //运行在UI线程中,更新当前进度条信息。被publishProgress调用    @Override    protected void onProgressUpdate(Integer... values) {        super.onProgressUpdate(values);        pb.setProgress(progress);        tvProgress.setText(progress + "%");    }}

布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="30dp"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="下载进度:"/>        <TextView            android:id="@+id/tv_progress"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="0"/>    </LinearLayout>    <ProgressBar        android:id="@+id/progress_bar"        style="@style/Widget.AppCompat.ProgressBar.Horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="50dp"        android:max="100"/>    <Button        android:id="@+id/btn_start"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="开始下载"/></LinearLayout>

Demo效果图:

总结:AsncyTask适合处理一些简单的异步任务,然而对于比较复杂的或多个同时存在异步任务,还是使用Handler异步消息机制更加方便和清晰。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表