Android:AsyncTask 模拟下载

package com.sjcs.zhao.sjcsapp;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

/**
 * Created by zhao on 2017/2/23.
 */

public class DownloadAsyncTask extends AsyncTask {

    ProgressDialog pd;
    private int count=0;
    private  Context context;
    public DownloadAsyncTask(Context context) {
        this.context=context;
        pd=new ProgressDialog(context);
    }

    @Override
    protected void onPreExecute() {
       pd.show();
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
       pd.dismiss();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        pd.setMessage("download"+  values[0] +"%");
    }

    @Override
    protected Boolean doInBackground(Void... voids) {

        try {
            while (true){
                count=doDownload();
                if(count==100){
                    break;
                }
            }
        } catch (InterruptedException e) {
            return false;
        }
        return true;
    }
    private int doDownload() throws InterruptedException {
        count=0;
        while (true){
            count=count+1;
            Thread.sleep(100);
            publishProgress(count);
            if (count==100){
                break;
            }
        }
        return count;
    }
}




你可能感兴趣的:(Android:AsyncTask 模拟下载)