How to show the progress bar in FTP download in async class in Android?

前端 未结 2 1528
天命终不由人
天命终不由人 2021-01-16 12:57

How to show the progress bar in FTP download in async class in android?

I\'ve tried many things but didn\'t get the progress bar. Here\'s my code, and I\'m calling

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-16 13:27

    You can do something like this..

    public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
    private ProgressDialog mProgressDialog;
    
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage("waiting 5 minutes..");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
            return mProgressDialog;
        default:
        return null;
        }
    }
    

    Then write an async task to update progress..

    private class DownloadZipFileTask extends AsyncTask {
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);
        }
    
        @Override
        protected String doInBackground(String... urls) {
            //Copy you logic to calculate progress and call
            publishProgress("" + progress);
            //Your code Here
        }
    
        protected void onProgressUpdate(String... progress) {        
        mProgressDialog.setProgress(Integer.parseInt(progress[0]));
        }
    
        @Override
        protected void onPostExecute(String result) {           
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        }
    }
    

    This is the Procedure to use Progress Dialog update with the AsyncTask, write your code in doInBackground(String...)

提交回复
热议问题