Android:如何使用DownloadManager暂停和继续? [英] Android : How to pause and resume using DownloadManager?

查看:1297
本文介绍了Android:如何使用DownloadManager暂停和继续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

this is my code and i want to add resumable feature but i couldn't is it posible?

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 DownloadManager和Downloads Content Provider进行以下实现:

I was able to implement Pause/Resume functionality using the following implementation via the Android DownloadManager and 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;
} 

这篇关于Android:如何使用DownloadManager暂停和继续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆