是否有可能取消/停止下载开始使用下载管理器? [英] Is it possible to cancel/stop a download started using DownloadManager?

查看:155
本文介绍了是否有可能取消/停止下载开始使用下载管理器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的下载管理器来下载一堆文件在我的应用程序。我无法弄清楚如何取消已入队通过下载管理器的下载。

I am using DownloadManager to download a bunch of files in my application. I am not able to figure out how to cancel the downloads which has been enqueued by downloadManager.

有两种可能性: 一个。用户可以手动取消它通过单击它在通知栏说。 湾取消并删除通过code下载。

There are two possibilities: a. User can manually cancel it say by clicking it in the notification bar. b. Cancel and remove the download through code.

我有以下的接收器定义的。

I have the following receiver defined.

<receiver 
        android:name=".DownloadStatusReceiver"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
            <action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED" />
     </intent-filter>
 </receiver> 

和在接收器

if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) {

    Constants.showLog(TAG, "Notification clicked");
    long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
    DownloadManager dm =(DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);             
    dm.remove(downloadId);

}

任何见解?

推荐答案

您可以通过调用它的删除(取消长通过下载管理器下载.. 。)方法。为此,你需要下载的ID。从我的经验也基本上有两种可靠的方法如何得到它:

You can cancel downloads via DownloadManager by calling its remove(long...) method. For this you need the ID of the download. From my experience there basically are two reliable ways how to get it:

  1. 记住排队(DownloadManager.Request)方法的返回值。
  2. 通过查询下载管理器的下载查询(DownloadManager.Query)方法。然后检索的ID返回光标,它们被存储在列名为 DownloadManager.COLUMN_ID
  1. Remember the return value of enqueue(DownloadManager.Request) method.
  2. Query the DownloadManager for downloads via query(DownloadManager.Query) method. Then retrieve the IDs from the returned Cursor, they are stored in the column named DownloadManager.COLUMN_ID.

广播接收器

这是我的经验,这是不可靠的通过的BroadcastReceiver 检索下载ID为行动 android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED (虽然广播始终发送)。

From my experience, it is not reliable to retrieve download ID via BroadcastReceiver for action android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED (though the broadcast is always sent).

  1. 获取下载标识从额外下载管理器。 EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS 不能正常工作。在某些设备上,它总是返回null。如果返回的东西在某些设备上,它是下载的ID启动的第一个。如果第一个下载完成后/取消,余下的下载通知返回null。
  2. 从获取额外 DownloadManager.EXTRA_DOWNLOAD_ID 并没有为这次行动工作的值。
  1. Getting download IDs from extra DownloadManager. EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS does not work properly. On some devices, it always return null. If it returns something on some devices, it is the ID of the download started first. And if the first download is finished/canceled, it returns null for notification of the remaining downloads.
  2. Getting a value from extra DownloadManager.EXTRA_DOWNLOAD_ID does not work for this action.

获取ID广播行动 android.intent.action.DOWNLOAD_COMPLETE 似乎可靠。你必须从额外得到它 DownloadManager.EXTRA_DOWNLOAD_ID 。需要注意的是广播发送,不仅为完成下载,这也是当您取消下载调用删除()发送

Getting ID in broadcast for action android.intent.action.DOWNLOAD_COMPLETE seems reliable. You have to get it from extra DownloadManager.EXTRA_DOWNLOAD_ID. Note that the broadcast is sent not only for completed download, it is also sent when you cancel download calling remove().

注意: 下载有时被在一个通知,有时会创建多个通知。我没能找出条件时,通知和不群。它似乎取决于许多因素,如OS版本,设备,下载标题,...以及在一般似乎相当未predictable

Note: Downloads are sometimes grouped in one notification, sometimes create multiple notifications. I wasn't able to figure out the conditions when notifications do and do not group. It seems to depend on many factors like OS version, device, download title, ... and in general seems rather unpredictable.

注意: 我测试过是否可以取消其他的应用程序的下载,它似乎并没有那么。虽然该ID是是在所有的应用程序唯一的数据库ID。呼叫删除()不取消其他应用程序的下载。

Note: I've tested whether you can cancel other app's download and it doesn't seem so. Even though that the IDs are database IDs that are unique across all apps. Calling remove() does not cancel another app's download.

这篇关于是否有可能取消/停止下载开始使用下载管理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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