如何在android中暂停downloadmanager

时间:2015-07-09 10:14:39

标签: android download resume

问题是我已经实现了android downloadmanager来从互联网上下载文件。我该如何暂停和恢复下载?我已经阅读了很多帖子并搜索了谷歌但找不到合适的答案。感谢

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

0 个答案:

没有答案