簡體   English   中英

2 分鍾后為 Android 下載管理器設置超時

[英]Set timeout for Android Download manager after 2 min

如何設置下載管理器超時以及在 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)

}

在上面的代碼中,如何使用下載管理器處理超時。

您可以使用 Handler.postDelayed(); 安排一個新線程在 x 毫秒后運行; 在您使用的預定線程中,您將使用 DownloadManager.query 訪問公開下載狀態的 Cursor object。 如果下載狀態指示下載未成功完成,您可以使用 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM