【问题标题】:How to capture cancel download event in android programmatically如何以编程方式在android中捕获取消下载事件
【发布时间】:2017-05-29 08:45:33
【问题描述】:

当文件开始下载时,我会显示进度条,如果完成,我会使用成功下载图标更改进度条。它工作得很好,但我面临的问题是,如果有人点击通知托盘中的取消按钮,它不会发送我创建的广播,以便在文件下载时接收广播。

我正在使用 DownloadManager 下载文件,正如我们在 marshmallow 中所知道的,我们可以选择下载文件,但如何捕获此取消按钮的事件。

这是我的代码:

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

【问题讨论】:

    标签: android android-6.0-marshmallow android-broadcast android-download-manager android-broadcastreceiver


    【解决方案1】:

    ACTION_DOWNLOAD_CLICKED 仍然会被触发,但是当下载被取消时,这里的光标不在查询中。

    kotlin 中的代码示例:

    private fun wasCanceled(downloadId: Long): Boolean { val cursor = downloadManager.query(DownloadManager.Query().setFilterById(downloadId)) return cursor == null || !cursor.moveToFirst() }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-07
      • 1970-01-01
      • 2010-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      相关资源
      最近更新 更多