在DownloadManager中取消下载

时间:2016-09-01 11:59:28

标签: android android-download-manager

我正在尝试使用以下代码从URL下载图像: -

public static void writeToDisk(Context context, @NonNull String imageUrl, @NonNull String downloadSubfolder) {
    Uri imageUri = Uri.parse(imageUrl);
    String fileName = imageUri.getPath();
    String downloadSubpath = downloadSubfolder + fileName;

    DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    DownloadManager.Request request = new DownloadManager.Request(imageUri);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDescription(imageUrl);
    request.allowScanningByMediaScanner();
    request.setDestinationUri(getDownloadDestination(downloadSubpath));

    downloadManager.enqueue(request);
}

我无法弄清楚如何在下载后取消下载。

1 个答案:

答案 0 :(得分:4)

使用enqueue方法获取

中的ID
long downloadID = downloadManager.enqueue(request);

然后,使用remove方法将downloadID传递给它。

downloadManager.remove(downloadID);
相关问题