private val downloadManager by lazy { getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager }
private var downloadId = 0L
fun downloadByManager() {
val downloadRequest = DownloadManager.Request(Uri.parse("https://study.163.com/pub/study-android-official.apk")).apply {
setAllowedOverRoaming(false)
setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
setTitle("Download Update")
setDescription("Download progress running")
setVisibleInDownloadsUi(true)
val file = File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "study163.apk")
setDestinationUri(Uri.fromFile(file))
}
downloadId = downloadManager.enqueue(downloadRequest)
registerReceiver(downloadReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
}
private val downloadReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val query = DownloadManager.Query()
query.setFilterById(downloadId)
val cursor = downloadManager.query(query)
if (!cursor.moveToFirst()) return
Log.d("--wh--", when (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
DownloadManager.STATUS_PAUSED -> "PAUSED"
DownloadManager.STATUS_PENDING -> "PENDING"
DownloadManager.STATUS_RUNNING -> "RUNNING"
DownloadManager.STATUS_SUCCESSFUL -> "SUCCESSFUL"
DownloadManager.STATUS_FAILED -> "FAILED"
else -> "===>"
})
}
}
Android DownloadManager
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 当react-native项目中有版本更新需求时,android会用到下载新版本需求,此时,可以通过封装Nativ...
- DownloadManager是Android系统提供给第3方应用的下载管理功能。 我用它去下载一个apk.文件 ...
- 现在做了一个下载,为了兼容性直接使用Android Sdk 自带的 DownloadManager 开发,在其他手...
- 17日出行归来,忙过了两天。今天先把自由行的攻略写出来,因为在出行前参考了很多网友的攻略,帮助很大。赠人玫瑰,手留...