当文件开始下载时,我显示进度条,如果完成,我更改了进度条并成功下载图标。它运行良好,但我面临的问题是,如果有人单击通知托盘中的取消按钮,它不会发送我创建的广播,以便在下载文件时接收广播。
我正在使用DownloadManager下载文件,正如我们所知,在棉花糖中,我们可以选择下载文件,但是如何捕获此取消按钮的事件。
这是我的代码:
<receiver android:name=".adapters.VideoListAdapter$VideoDownloadedReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
<action android:name="android.intent.action.DOWNLOAD_CANCEL" />
</intent-filter>
</receiver>
public static class VideoDownloadedReceiver extends BroadcastReceiver implements AsyncResponse {
@Override
public void onReceive(Context context, Intent intent) {
//perform event
//works well if files gets completed but not if download gets cancel
}
}
ACTION_DOWNLOAD_CLICKED仍将被触发,但是当下载被取消时,查询中的光标不是
。Kotlin 中的代码示例:
private fun wasCanceled(downloadId: Long): Boolean {
val cursor = downloadManager.query(DownloadManager.Query().setFilterById(downloadId))
return cursor == null || !cursor.moveToFirst()
}