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;