Android N-下载管理器通知取消按钮 [英] Android N - Download Manager Notification Cancel Button

查看:196
本文介绍了Android N-下载管理器通知取消按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Android N has a new Cancel Button in the Download Manager Notification.

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

I would like to excecute some code in my app to stop a progressbar when the user presses this button. If any, which method is called?

请注意,仅当用户单击通知本身时,才触发Intent筛选器操作DownloadManager.ACTION_NOTIFICATION_CLICKED,而不是在单击取消"按钮时触发.

Please also note that the Intent filter action DownloadManager.ACTION_NOTIFICATION_CLICKED is triggered only when the user clicks on the notification itself, not when he/she clicks of the Cancel button.

 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
            }       
        }
    };

谢谢.

推荐答案

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

I had the same problem in my app where I had to handle the Cancel button on the downloads notification and downloads deletion from the native Downloads app.

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

Turns out that if you register a receiver with the intent filter: DownloadManager.ACTION_DOWNLOAD_COMPLETE, it's always called when the cancel or downloads deletion is initiated.

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

好吧,这很简单:

  1. Intent data获取已取消下载的下载管理器ID(dmid),该下载管理器ID作为参数传递给BroadcastReceiverhandleReceive函数.
  2. 使用dmid查询DownloadManager的状态.
  3. 对于该dmid,DownloadManager将返回null,或者对于所述下载,DownloadManager.STATUS_SUCCESSFUL列的值将为false.
  4. 一旦您知道这一点,您就可以做任何您想做的事!
  1. Get the Download Manager ID (dmid) of the canceled download from the Intent data that is passed as an argument to the handleReceive function of your BroadcastReceiver.
  2. Using that dmid query the DownloadManager for its status.
  3. The DownloadManager would either return null for that dmidor the value for DownloadManager.STATUS_SUCCESSFUL column would be false for the said download.
  4. Once you know this, you can do whatever you want!

作为参考,您可以在这里查看我的操作方式:

For reference you can see how I did it here:

  1. AndroidManifest.xml 中我的接收者的声明: https://github.com/edx/edx-app-android/blob/8a75d0dba6b8570956eac5c21c99ecd5020c81ae/OpenEdXMobile/AndroidManifest.xml#L265-L271
  2. 我的接收方处理这种情况的实际代码:
  1. My Receiver's declaration in AndroidManifest.xml: https://github.com/edx/edx-app-android/blob/8a75d0dba6b8570956eac5c21c99ecd5020c81ae/OpenEdXMobile/AndroidManifest.xml#L265-L271
  2. My Reciever's actual code that handles this case: https://github.com/edx/edx-app-android/blob/d986a9ab64e7a0f999024035ec6fcbdb3428f613/OpenEdXMobile/src/main/java/org/edx/mobile/module/download/DownloadCompleteReceiver.java#L50-L62

这篇关于Android N-下载管理器通知取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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