How to add ProgressDialog

穿精又带淫゛_ 提交于 2019-11-28 14:25:19

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

Use this simple code @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);
    }

}

This article can be useful for you:

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

Where inside the run() method of your thread you can invoke a function like this:

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

Doing this way, you have a more accurate feedback about real progress of you download (byte per byte).

See there are actually 4 methods of AsyncTask:

  1. onPreExecute() - you can do some pre execution task here.
  2. doInBackground() - you can perform some background work here.
  3. onPostExecute() - you can perform post execution task here. Means like displaying data in ListView, update TextView, etc.
  4. onProgressUpdate() - To update UI while background operation is going on.

So in your case, you can show progress dialog or progress bar inside onPreExecute() method of AsyncTask and dismiss(() the same inside onPostExecute().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!