Progress Dialog during download

2

I use the following method to download an image.

  public void downloadFile(String uRl, String title, String description) {
    File direct = new File(Environment.getExternalStorageDirectory()
            + "/FPSWallpapers");

    if (!direct.exists()) {
        direct.mkdirs();
    }

    DownloadManager mgr = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);

    Uri downloadUri = Uri.parse(uRl);
    DownloadManager.Request request = new DownloadManager.Request(
            downloadUri);

    request.setAllowedNetworkTypes(
            DownloadManager.Request.NETWORK_WIFI
                    | DownloadManager.Request.NETWORK_MOBILE)
            .setAllowedOverRoaming(false).setTitle(title)
            .setDescription(description)
            .setDestinationInExternalPublicDir("/FPSWallpapers", title);

    mgr.enqueue(request);

}

It works normally, it downloads and saves the image. But I wanted to show a progressDialog while the download is done, and then display a message saying it was collapsed.

I tried the following:

    public void downloadFile(String uRl, String title, String description) {
    ProgressDialog progress = new ProgressDialog(mContext);

    while (!checkDownloadComplete.isDownloadComplete) {
        progress.setMessage("Downloading Music :) ");
        progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progress.setIndeterminate(true);
    }
    File direct = new File(Environment.getExternalStorageDirectory()
            + "/FPSWallpapers");

    if (!direct.exists()) {
        direct.mkdirs();
    }

    DownloadManager mgr = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);

    Uri downloadUri = Uri.parse(uRl);
    DownloadManager.Request request = new DownloadManager.Request(
            downloadUri);

    request.setAllowedNetworkTypes(
            DownloadManager.Request.NETWORK_WIFI
                    | DownloadManager.Request.NETWORK_MOBILE)
            .setAllowedOverRoaming(false).setTitle(title)
            .setDescription(description)
            .setDestinationInExternalPublicDir("/FPSWallpapers", title);

    mgr.enqueue(request);
    progress.dismiss();

}

and the Broadcastreceiver class:

public class checkDownloadComplete  extends BroadcastReceiver {

public static boolean isDownloadComplete= false;

@Override
public void onReceive(Context context, Intent intent) {
    isDownloadComplete = true;
    Log.i("Download completed?", String.valueOf(isDownloadComplete));
}

}

But the app stays in some loop that hangs. How to show a progressdialog while downloading?

PS: The required permissions have been given;

    
asked by anonymous 17.11.2017 / 16:23

1 answer

0

Start the progress when you start the download and dismiss it when you complete it. Remove this while. Perhaps ideal would be to create two methods, passing the context to start the progress and in the end, remove it from the screen.

Being started where the while is. Being your end, so I understood, in onReceive, of your broadcast class.

    
06.03.2018 / 15:06