0

I'm trying to download file using DownloadManager in Async Task. Here's the doInBackground() method

protected Boolean doInBackground(String... params) {
        DownloadManager mgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        //String DIRECTORY_DOWNLOADS = m_context.getExternalFilesDir(null).getAbsolutePath();

        Uri uri = Uri.parse(params[0]);
        typ_mapy = Integer.parseInt(params[2]);
        File download = new File(getExternalFilesDir(null) ,params[1]);
        if(download.exists()){
            info = "Existuje";
            nazev_souboru=params[1];
            return false;
        }else{

            try {
                long lastDownload = mgr.enqueue(new DownloadManager.Request(uri)
                                    .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI)
                                    .setAllowedOverRoaming(false)
                                    .setNotificationVisibility(Request.VISIBILITY_HIDDEN)
                                    .setDestinationInExternalFilesDir(m_context,"/",params[1]));
                    Cursor c = mgr.query(new DownloadManager.Query().setFilterById(lastDownload));
                    while(c.moveToFirst())
                    {


                        publishProgress(String.valueOf(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))), String.valueOf(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))));
                        /*if(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))>=c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))){
                            c.close();
                            return true;
                        }*/
                        c.close();
                        c = mgr.query(new DownloadManager.Query().setFilterById(lastDownload));
                        c.moveToFirst();
                    }

            } catch (Exception e) {
                return false;
            }
            return true;
        }
    }

You can see that I have a comment there. I thought that that condition will stop the loop, when download is finished, but when it's there, it simply calls onPostExecute() immediatly (although the download is running and finishes).

The problem is it stops calling the onProgressUpdate() where I update my progress bar.

Is there any way how to keep it in the while loop so the progress bar gets updated? If it's like this, it stays there in an endless loop. If I uncomment the condition, it finishes instantly.

EDIT: I solved it by changing the loop like this:

boolean downloading = true;
                    while (downloading) {

                        DownloadManager.Query q = new DownloadManager.Query();
                        q.setFilterById(lastDownload);

                        Cursor cursor = mgr.query(q);
                        cursor.moveToFirst();
                        long bytes_downloaded = cursor.getLong(cursor
                                .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                        long bytes_total = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                        publishProgress(String.valueOf(bytes_total), String.valueOf(bytes_downloaded));
                        if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                            downloading = false;
                        }
                    }

Answer found here: Show Download progress inside activity using DownloadManager

Community
  • 1
  • 1
ezpzlmnsqz1337
  • 394
  • 1
  • 5
  • 16

1 Answers1

2

Hi you have to register a broadcast receiver to receive downloading complete you can refer https://github.com/commonsguy/cw-android/blob/master/Internet/Download/src/com/commonsware/android/download/DownloadDemo.java

Ravi Kant
  • 820
  • 6
  • 13