【问题标题】:Android : How to pause and resume using DownloadManager?Android:如何使用 DownloadManager 暂停和恢复?
【发布时间】:2016-10-07 07:14:12
【问题描述】:

这是我的代码,我想添加可恢复的功能,但我不能这样做吗?

downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse("http://download.thinkbroadband.com/20MB.zip");
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);

//Restrict the types of networks over which this download may proceed.
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
//Set whether this download may proceed over a roaming connection.
request.setAllowedOverRoaming(false);
//Set the title of this download, to be displayed in notifications (if enabled).
request.setTitle("My Data Download");
//Set a description of this download, to be displayed in notifications (if enabled)
request.setDescription("Android Data download using DownloadManager.");
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS,"20MB.zip");

//Enqueue a new download and same the referenceId
downloadReference = downloadManager.enqueue(request);

【问题讨论】:

    标签: android download android-download-manager download-manager


    【解决方案1】:

    我能够通过 Android DownloadManager 和 Downloads Content Provider 使用以下实现来实现暂停/恢复功能:

    private boolean resumeDownload(Context context, String downloadTitle) {
        int updatedRows = 0;        
    
        ContentValues resumeDownload = new ContentValues();
        resumeDownload.put("control", 0); // Resume Control Value
    
        try {
            updatedRows = context
                .getContentResolver()
                .update(Uri.parse("content://downloads/my_downloads"),
                    resumeDownload,
                    "title=?",
                    new String[]{ downloadTitle });
        } catch (Exception e) {
            Log.e(TAG, "Failed to update control for downloading video");
        }
    
        return 0 < updatedRows;
    }
    
    private boolean pauseDownload(Context context, String downloadTitle) {
        int updatedRows = 0;
    
        ContentValues pauseDownload = new ContentValues();
        pauseDownload.put("control", 1); // Pause Control Value
    
        try {
            updatedRows = context
                .getContentResolver()
                .update(Uri.parse("content://downloads/my_downloads"),
                    pauseDownload,
                    "title=?",
                    new String[]{ downloadTitle });
        } catch (Exception e) {
            Log.e(TAG, "Failed to update control for downloading video");
        }
    
        return 0 < updatedRows;
    } 
    

    【讨论】:

    • 女巫网址是你的意思吗?
    • 有趣的变体。但是,恐怕它将应用于所有下载事件。
    • 嗨,@jjh 你能帮我恢复下载吗?
    猜你喜欢
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    相关资源
    最近更新 更多