Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

android - How to add ProgressDialog

I am downloading a file from dropbox which is taking a few seconds. I want to add a ProgressDialog for the download but I don't know how to do that.

public class DownloadFile extends AsyncTask<Void, Long, Boolean> {
    DownloadFile(Context context ,DropboxAPI<?> mApi ,String dropboxpath,String   sdpath,int pos,int s,ArrayList<String> folder) throws DropboxException {
        FileOutputStream mFos;
        File file=new File(sdpath);
        String path = dropboxpath;
        try{
            mFos = new FileOutputStream(file);
            mApi.getFile(path, null, mFos, null);
        }catch (Exception e) {
            // TODO: handle exception
        }
    } 

    @Override
    protected Boolean doInBackground(Void... params) {
        // TODO Auto-generated method stub
        return null;
    }   
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Do it this way:

public final class DownloadFile extends AsyncTask<Void, Long, Boolean> {

private Context context;
private ProgressDialog progressDialog;

public DownloadFile (Context context) {
    this.context = context;
}

/* 
 * @see android.os.AsyncTask#onPreExecute()
 */
@Override
protected void onPreExecute() {
    try {
        progressDialog = ProgressDialog.show(context, "", "message", true);
    } catch (final Throwable th) {
        //TODO
    }
}

/* 
 * @see android.os.AsyncTask#doInBackground(Params[])
 */
@Override
protected Boolean doInBackground(Void... arg0) {
    //do something
}

    @Override
protected void onProgressUpdate(String... progress) {
    //do something
    super.onProgressUpdate(progress);
}

/* 
 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
 */
@Override
protected void onPostExecute(Boolean result) {
    progressDialog.dismiss();
} }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...