我正在从Dropbox下载文件,这需要花费几秒钟的时间。我想为下载添加ProgressDialog,但是我不知道该怎么做。

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;
    }
}

最佳答案

这样做:

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();
} }

07-26 07:51