设置Android下载管理器超时2分钟后

to94eoyn  于 2023-01-03  发布在  Android
关注(0)|答案(1)|浏览(93)

bounty将在7天后过期。回答此问题可获得+100的声誉奖励。Mano正在寻找此问题的更详细的答案

如何设置下载管理器超时以及在2分钟后启用或禁用下载管理器。

public void downloadFile(String url) {
  val downloadManager = this.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

            val downloadUri = Uri.parse(url)

            val request = DownloadManager.Request(downloadUri).apply {
                try {
                    setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
                        .setAllowedOverRoaming(true)
                        .setTitle(url.substring(url.lastIndexOf("/") + 1))
                        // .setDescription("abc")
                        .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                        .setDestinationInExternalPublicDir(
                            Environment.DIRECTORY_DOWNLOADS,
                            url.substring(url.lastIndexOf("/") + 1)
                        )
                } catch (e: Exception) {
                    e.printStackTrace()
                }

            }
            //TODO to get the Downloading status
            val downloadId = downloadManager.enqueue(request)
            val query = DownloadManager.Query().setFilterById(downloadId)

}

在上面的代码如何处理下载管理器超时.

ovfsdjhp

ovfsdjhp1#

您可以使用Handler.postDelayed()将新线程调度为在x毫秒后运行;在该调度线程中,您将使用DownloadManager.query访问Cursor对象,该对象显示下载状态。如果下载状态指示下载未成功完成,您可以使用DownloadManager.remove()取消它。
我测试了这个方法:

private fun tryDownloadFileButCancelIfTimeout(url: String, millisecondsToTimeoutAfter : Long) {
    val downloadManager = this.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
    val downloadUri = Uri.parse(url)
    val request = DownloadManager.Request(downloadUri).apply {
        try {
            setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
                .setAllowedOverRoaming(true)
                .setTitle(url.substring(url.lastIndexOf("/") + 1))
                .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
                .setDestinationInExternalPublicDir(
                    Environment.DIRECTORY_DOWNLOADS,
                    url.substring(url.lastIndexOf("/") + 1)
                )
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
    val downloadId = downloadManager.enqueue(request)
    // Schedule a new thread that will cancel the download after millisecondsToTimeoutAfter milliseconds
    Handler(Looper.getMainLooper()).postDelayed({
        val downloadQuery = DownloadManager.Query().setFilterById(downloadId)
        val cursor = downloadManager.query(downloadQuery)
        val downloadStatusColumn = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)
        cursor.moveToPosition(0)
        val downloadStatus = cursor.getInt(downloadStatusColumn)
        if (downloadStatus != DownloadManager.STATUS_SUCCESSFUL) {
            downloadManager.remove(downloadId)
        }

    }, millisecondsToTimeoutAfter)
}

发布我用来测试的代码(可以导入到Android Studio,如果你喜欢,一个简单的应用程序,一个下载按钮,开始下载,并在五秒钟内取消):
https://github.com/hishamhijjawi/DownloadCancelDemoStackOverflow

相关问题