简体   繁体   English

系统重启后如何停止正在运行的下载

[英]How to stop a running download after a system reboot

Here is the situation: 情况如下:

  1. I start a download 我开始下载
  2. I restart the device (to test the robustness of the app) 我重新启动设备(以测试应用程序的健壮性)
  3. I use a boot receiver to be notified of the restart 我使用启动接收器来通知重启
  4. In the onReceive method I call the clearDownloadsAfterReboot() method: 在onReceive方法中,我调用clearDownloadsAfterReboot()方法:

      @Override public void onReceive(final Context context, Intent intent) { clearDownloadsAfterReboot(context); } 
  5. The clearDownloadsAfterReboot() method get called and tries to remove the downloads like so: 调用clearDownloadsAfterReboot()方法并尝试删除下载内容,如下所示:

     private void clearDownloadsAfterReboot(final Context context){ //Added delay to make sure download has started new Handler().postDelayed(new Runnable() { @Override public void run() { DownloadManager manager = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE); DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById (DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PENDING|DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_PAUSED); Cursor c = manager.query(query); // -------> c.getCount() returns 0 despite the download being visible while(c.moveToNext()) { //This never executes because c has a count of 0 int removedInt = manager.remove(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID))); } } }, 45000); } 
  6. The cursor returns 0 so the remove() method is never called, but, I can see the download running in the status bar so there is at least 1 download running. 游标返回0,所以从不调用remove()方法,但是,我可以在状态栏中看到下载正在运行,因此至少有1个下载正在运行。

My questions in a nutshell: How do I stop running downloads after a reboot? 简而言之,我的问题是:重新启动后如何停止运行下载? And, why would the manager.query() method return a cursor which has 0 results when there is a download running? 而且,为什么在运行下载时manager.query()方法返回一个结果为0的游标?

I know there are other question regarding stopping downloads but these have not been able to solve my issue. 我知道还有其他有关停止下载的问题,但是这些问题无法解决我的问题。

Thanks in advance. 提前致谢。

Credit for the first part of this answer goes to @Lukesprog 此答案第一部分的功劳归@Lukesprog

I needed to use setFilterByStatus() instead of setFilterById() . 我需要使用setFilterByStatus()而不是setFilterById() After exchanging these two methods I was able to successfully stop the download and the manager.query() method returned a cursor containing the correct amount of downloads. 交换这两种方法后,我能够成功停止下载,并且manager.query()方法返回了一个包含正确下载量的游标。

However, for some reason the notification which showed the progress of the download didn't clear after the download had been stopped. 但是,由于某种原因,在停止下载后,不会清除显示下载进度的通知。 In order to clear the notification you need to call manager.remove(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID))); 为了清除通知,您需要致电manager.remove(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID))); twice . 两次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM