1

The question is I have implemented android downloadmanager to download files from internet. how can i pause and resume the download? i have read lots of posts and searched google but could not find a working answer. Thanks

            DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(url + fileName));

            downloadRequest.setTitle("Downloading: ");
            downloadRequest.setDescription("Description");
            downloadRequest.allowScanningByMediaScanner();
            downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

            downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,applicationApk);

            manager = (DownloadManager) downloadActivity.getSystemService(Context.DOWNLOAD_SERVICE);
            downloadId = manager.enqueue(downloadRequest);



            new Thread(new Runnable()
            {
                @Override
                public void run()
                {

                    int fileSize = size;

                    downloadProgress.setProgress(0);
                    downloadProgress.setMax(fileSize);
                    sizeInBytes = fileSize * 1024 * 1024;

                    DownloadManager.Query q = new DownloadManager.Query();
                    //q.setFilterById(downloadId);

                    downloading = true;
                    while (downloading)
                    {
                        Cursor cursor = manager.query(q);
                        if (cursor.moveToFirst())
                        {
                            int sizeIndex = cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
                            int downloadedIndex = cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
                            long size = cursor.getInt(sizeIndex);
                            long downloaded = cursor.getInt(downloadedIndex);

                            if (size != -1) dl_progress = downloaded * fileSize / size;
                        }

                        downloadProgress.setProgress((int) dl_progress);

                        if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL)
                        {
                            downloading = false;

                        }


                        cursor.close();

                    }

                }
            }).start();
Rafa
  • 467
  • 4
  • 18
  • possible duplicate of [DownloadManager with manually pause and resume](http://stackoverflow.com/questions/11297298/downloadmanager-with-manually-pause-and-resume) – Karol Żygłowicz Jul 09 '15 at 10:23
  • Mark as duplicated. I advise you to not use android DownloadManager. I had a lot of problems with it. Its very buggy (for example http://www.hafizpariabi.com/2013/05/workaround-bug-in-androids.html) – Karol Żygłowicz Jul 09 '15 at 10:25

0 Answers0