Android N在下载管理器通知中有一个新的取消按钮。
我想在我的应用程序中执行一些代码,以便在用户按下此按钮时停止进度条。如果有的话,调用哪种方法?
另请注意,Intent过滤器操作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
}
}
};
提前致谢。
我在我的应用程序中遇到了同样的问题,我必须处理下载通知上的“取消”按钮并从本机下载应用程序下载删除。
事实证明,如果您使用intent过滤器注册接收器:DownloadManager.ACTION_DOWNLOAD_COMPLETE,则在启动取消或下载删除时始终会调用它。
那么,如何区分下载完成和下载删除?
嗯,这很简单:
dmid
获取已取消下载的下载管理器ID(Intent data
),该handleReceive
作为参数传递给BroadcastReceiver
的dmid
函数。dmid
查询DownloadManager的状态。DownloadManager.STATUS_SUCCESSFUL
or返回null,false
列的值将为https://github.com/edx/edx-app-android/blob/8a75d0dba6b8570956eac5c21c99ecd5020c81ae/OpenEdXMobile/AndroidManifest.xml#L265-L271进行上述下载。作为参考,您可以在这里看到我是如何做到的:
Malko,我没有找到解决方案,但我同时使用以下解决方法。我使用Android Handler每10秒运行一次resetProgressIfNoOngoingDMRequest(),如下所示:
public int numberOfOngoingDMRequest() {
cursor = downloadManager.query(new Query());
int res = cursor.getCount();
cursor.close();
return res;
}
public boolean resetProgressIfNoOngoingDMRequest() {
if (numberOfOngoingDMRequest() == 0) {
refreshUpdateAllButton(false);
resetEpisodesDownloadIds();
act.misc.notifyEpisodesDataSetChanged();
return true;
}
return false;
}
不太好但是它能完成这项工作。我只在应用程序位于前台时执行此操作。
另一种解决方案是使用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();
}
}
}
}
}
});
看到答案here
我的解决方案与MiaN KhaLiD非常相似。单击“取消”时,DownloadManager.ACTION_DOWNLOAD_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;
}