Android N-下载管理器通知取消按钮 [英] Android N - Download Manager Notification Cancel Button
问题描述
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.
那么,如何区分下载完成和下载删除?
好吧,这很简单:
- 从
Intent data
获取已取消下载的下载管理器ID(dmid
),该下载管理器ID作为参数传递给BroadcastReceiver
的handleReceive
函数. - 使用
dmid
查询DownloadManager的状态. - 对于该
dmid
,DownloadManager将返回null,或者对于所述下载,DownloadManager.STATUS_SUCCESSFUL
列的值将为false
. - 一旦您知道这一点,您就可以做任何您想做的事!
- Get the Download Manager ID (
dmid
) of the canceled download from theIntent data
that is passed as an argument to thehandleReceive
function of yourBroadcastReceiver
. - Using that
dmid
query the DownloadManager for its status. - The DownloadManager would either return null for that
dmid
or the value forDownloadManager.STATUS_SUCCESSFUL
column would befalse
for the said download. - Once you know this, you can do whatever you want!
作为参考,您可以在这里查看我的操作方式:
For reference you can see how I did it here:
- AndroidManifest.xml 中我的接收者的声明: https://github.com/edx/edx-app-android/blob/8a75d0dba6b8570956eac5c21c99ecd5020c81ae/OpenEdXMobile/AndroidManifest.xml#L265-L271
- 我的接收方处理这种情况的实际代码:
- My Receiver's declaration in AndroidManifest.xml: https://github.com/edx/edx-app-android/blob/8a75d0dba6b8570956eac5c21c99ecd5020c81ae/OpenEdXMobile/AndroidManifest.xml#L265-L271
- 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屋!