Android N - 下载管理器通知取消按钮

u2g*_*les 5 android cancel-button android-notifications android-download-manager

Android N 在下载管理器通知中有一个新的取消按钮。

我想在我的应用程序中执行一些代码,以在用户按下此按钮时停止进度条。如果有的话,调用哪个方法?

另请注意,意图过滤器操作 DownloadManager.ACTION_NOTIFICATION_CLICKED 仅在用户单击通知本身时触发,而不是在他/她单击“取消”按钮时触发。

 if_downloadManager = new IntentFilter();
    if_downloadManager.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    if_downloadManager.addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED);

    br_downloadManager = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                ....
            }

            if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) {
                // This code is not executed when the user presses the Cancel Button in the Download Manager Notification
            }       
        }
    };
Run Code Online (Sandbox Code Playgroud)

提前致谢。

Mia*_*LiD 1

我在我的应用程序中遇到了同样的问题,我必须处理下载通知上的取消按钮并从本机下载中删除下载应用程序中删除下载。

事实证明,如果您使用意图过滤器注册接收器:DownloadManager.ACTION_DOWNLOAD_COMPLETE,则在启动取消或下载删除时始终会调用它。

那么,如何区分下载完成和下载删除呢?

嗯,这很简单:

  1. dmid获取已取消下载的下载管理器 ID ( ) Intent data,该 ID 作为参数传递给handleReceive您的函数BroadcastReceiver.
  2. 使用那个dmid查询 DownloadManager 的状态。
  3. DownloadManager 要么返回 null,要么列dmid的值DownloadManager.STATUS_SUCCESSFULfalse所述下载。
  4. 一旦你知道了这一点,你就可以做任何你想做的事了!

作为参考,您可以在这里查看我是如何做到的:

  1. 我的接收者在AndroidManifest.xml中的声明: https://github.com/edx/edx-app-android/blob/8a75d0dba6b8570956eac5c21c99ecd5020c81ae/OpenEdXMobile/AndroidManifest.xml#L265-L271
  2. 我的接收器处理这种情况的实际代码: https://github.com/edx/edx-app-android/blob/d986a9ab64e7a0f999024035ec6fcbdb3428f613/OpenEdXMobile/src/main/java/org/edx/mobile/module/download/DownloadCompleteReceiver.java #L50-L62

  • 请显示代码,而不是对您的存储库的引用。特别是因为您的代码中有接口和实现,所以无论谁访问这些链接,最终都会跳转到您的库中,只是为了弄清楚“getDownload(dmid)”是如何实现的 (3认同)