美文网首页
Android:AsyncTask 模拟下载

Android:AsyncTask 模拟下载

作者: lovingMJ | 来源:发表于2017-02-24 01:42 被阅读0次
    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<Void,Integer,Boolean> {
    
        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 模拟下载

          本文链接:https://www.haomeiwen.com/subject/vdviwttx.html