Android N-下载管理器通知取消按钮
Android N在下载管理器通知中有一个新的取消按钮 我想在我的应用程序中添加一些代码,以便在用户按下此按钮时停止progressbar。如果有,调用哪个方法 还请注意,仅当用户单击通知本身时,而不是当他/她单击“取消”按钮时,才会触发单击的意图筛选器操作DownloadManager.action_NOTIFICATION_Android N-下载管理器通知取消按钮,android,android-notifications,android-download-manager,cancel-button,Android,Android Notifications,Android Download Manager,Cancel Button,Android N在下载管理器通知中有一个新的取消按钮 我想在我的应用程序中添加一些代码,以便在用户按下此按钮时停止progressbar。如果有,调用哪个方法 还请注意,仅当用户单击通知本身时,而不是当他/她单击“取消”按钮时,才会触发单击的意图筛选器操作DownloadManager.action_NOTIFICATION_ if_downloadManager = new IntentFilter(); if_downloadManager.addAction(DownloadM
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
}
}
};
提前感谢。Malko,我没有找到解决方案,但同时我正在使用以下解决方法。 我使用Android处理程序每10秒运行一次resetProgressIfNoOngoingDMRequest(),如下所示:
虽然不太好,但它很管用。只有当应用程序位于前台时,我才会这样做。另一个解决方案是使用
ContentObserver
下载管理器的内容uri应为content://downloads/my_downloads
,我们可以监视此数据库的更改。当您使用下载id开始下载时,将创建一行content://downloads/my_downloads/{downloadId}
。我们可以检查此光标以了解此任务是否已取消。如果返回的游标为空或null,数据库中找不到记录,则用户将取消此下载任务
// get the download id from DownloadManager#enqueue
getContentResolver().registerContentObserver(Uri.parse("content://downloads/my_downloads"),
true, new ContentObserver(null) {
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
if (uri.toString().matches(".*\\d+$")) {
long changedId = Long.parseLong(uri.getLastPathSegment());
if (changedId == downloadId[0]) {
Log.d(TAG, "onChange: " + uri.toString() + " " + changedId + " " + downloadId[0]);
Cursor cursor = null;
try {
cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
Log.d(TAG, "onChange: running");
} else {
Log.w(TAG, "onChange: cancel");
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
}
});
查看答案我在应用程序中遇到了相同的问题,我必须处理下载通知上的取消按钮,并从本机下载应用程序中删除下载 结果表明,如果使用intent筛选器注册接收器:DownloadManager.ACTION\u DOWNLOAD\u COMPLETE,则在启动“取消”或“下载”删除操作时,始终会调用它 那么,如何区分下载完成和下载删除? 嗯,很简单:
意图数据
中获取已取消下载的下载管理器ID(dmid
),该数据作为参数传递给BroadcastReceive
的HandlerReceive
函数dmid
查询DownloadManager的状态dmid
返回null,或为DownloadManager返回值。对于所述下载,STATUS\u SUCCESSFUL
列将为false
我有和米安·哈立德非常相似的解决方案。单击“取消”时,DownloadManager.ACTION\u DOWNLOAD\u COMPLETE始终为接收器。我在接收器中这样做:
Cursor cursor = downloadManager.query(query);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status;
try {
status = cursor.getInt(columnIndex);
KLog.d("download status = " + status);
} catch (CursorIndexOutOfBoundsException e) {
KLog.d("cancelled from notification");
return;
}
你找到解决办法了吗?我也有同样的问题。请显示代码,而不是对存储库的引用。特别是因为代码中有接口和实现,所以无论谁访问这些链接,最终都会跳进库中,只是为了弄清楚
getDownload(dmid)
是如何实现的
Cursor cursor = downloadManager.query(query);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status;
try {
status = cursor.getInt(columnIndex);
KLog.d("download status = " + status);
} catch (CursorIndexOutOfBoundsException e) {
KLog.d("cancelled from notification");
return;
}