Android Background Processing

Sep 23 2022 · Kotlin 1.6, Android 12, Android Studio Chipmunk 2021.2.1

Part 2: Use WorkManager in Complex Apps

12. Use DownloadManager

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 11. Challenge - WorkManager Next episode: 13. Challenge - DownloadManager

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 12. Use DownloadManager

The student materials have been reviewed and are updated as of SEPTEMBER 2022.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You’ve upgraded the starter project with a lot of fun features, using the WorkManager. One of the key features of the project is downloading images. and the simplest way to download an image is to use an HTTP connection manually.

val constraints = Constraints.Builder()
    .setRequiredNetworkType(NetworkType.NOT_ROAMING)
    .setRequiresBatteryNotLow(true)
    .setRequiresStorageNotLow(true)
    .build()

val localImageCheckWorker = OneTimeWorkRequestBuilder<LocalImageCheckWorker>()
    .setInputData(workDataOf("image_path" to imageUrl))
    .setConstraints(constraints)
    .build()

val workManager = WorkManager.getInstance(requireActivity())
workManager.enqueue(localImageCheckWorker)

workManager.getWorkInfoByIdLiveData(localImageCheckWorker.id)
val isDownloaded = workInfo.outputData.getBoolean("is_downloaded", false)

if (!isDownloaded) {
  val file = File(requireContext().externalMediaDirs.first(), imageUrl)
  
}
val request = DownloadManager.Request(Uri.parse("$BASE_URL/files/$imageUrl"))
    .setTitle("Image download")
    .setDescription("Downloading")
    .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
    .setDestinationUri(Uri.fromFile(file))
    .setAllowedOverMetered(true)
    .setAllowedOverRoaming(false)
val downloadManager = requireContext().getSystemService(DownloadManager::class.java)
downloadManager?.enqueue(request)