1

我正在从保管箱下载一个文件,这需要几秒钟。我想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;
    }   
}
4

4 回答 4

3

这样做:

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();
} }
于 2012-07-05T07:44:19.687 回答
0

看到AsyncTask其实有4个方法:

  1. onPreExecute()- 你可以在这里做一些预执行任务。
  2. doInBackground()- 您可以在这里执行一些后台工作。
  3. onPostExecute()- 您可以在这里执行后期执行任务。比如在 ListView 中显示数据、更新 TextView 等。
  4. onProgressUpdate()- 在后台操作进行时更新 UI。

因此,在您的情况下,您可以在 AsyncTask 的方法中显示进度对话框或进度条onPreExecute(),并在内部显示相同的 dismiss(() onPostExecute()

于 2012-07-05T07:42:48.797 回答
0

使用这个简单的代码@sachin

public class DownloadFile extends AsyncTask<Void, Void, Void> {

    Home home;
    ProgressDialog dialog = null;

    public DownloadFile(Home home) {
        // TODO Auto-generated constructor stub
        this.home = home;
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        //Call hare method for download
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
         dialog.dismiss();  

    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
         dialog = ProgressDialog.show(home, "Downloading......", "", true);
    }

}
于 2012-07-05T07:46:43.977 回答
0

这篇文章可能对您有用:

http://huuah.com/android-progress-bar-and-thread-updating/

在线程的 run() 方法中,您可以调用如下函数:

 public boolean download(String url, String path, String fileName, Handler progressHandler) {
    try {
        URL sourceUrl = new URL(formatUrl(url));
        if (fileName == null || fileName.length() <= 0) {
            fileName = sourceUrl.getFile();
        }
        if (fileName == null || fileName.length() <= 0) {
            throw new Exception("EMPTY_FILENAME_NOT_ALLOWED");
        }
        File targetPath = new File(path);
        targetPath.mkdirs();
        if (!targetPath.exists()) {
            throw new Exception("MISSING_TARGET_PATH");
        }
        File file = new File(targetPath, fileName);
        URLConnection ucon = sourceUrl.openConnection();
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayBuffer baf = new ByteArrayBuffer(100);
        int current = 0;
        int totalSize = ucon.getContentLength();
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
            // BEGIN - Handler feedback
            if (progressHandler != null && (baf.length() % 100) == 0) {
                Message msg = progressHandler.obtainMessage();
                Bundle b = new Bundle();
                if (totalSize > 0) {
                    b.putInt("total", totalSize);
                    b.putInt("step", baf.length());
                    b.putBoolean("working", true);
                }
                msg.setData(b);
                progressHandler.handleMessage(msg);
            }
            // END
        }
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baf.toByteArray());
        fos.close();
        // BEGIN - Handler feedback
        if (progressHandler != null) {
            Message msg = progressHandler.obtainMessage();
            Bundle b = new Bundle();
            if (totalSize > 0) {
                b.putInt("total", 0);
                b.putInt("step", 0);
                b.putBoolean("working", false);
            }
            msg.setData(b);
            progressHandler.handleMessage(msg);
        }
        // END
        return file.exists();
    }

这样做,您可以更准确地反馈下载的实际进度(每个字节的字节数)。

于 2012-07-05T07:47:50.873 回答