0

What are alternative approaches to Download file other than AsyncTask - need to replace asynctask as it is deprecated.

Thanks in advance.

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122

4 Answers4

3

WorkManager is used for background tasks. You should create a Worker class e.g. FileDownloadWorker. If you need to do the file download asynchronously, you can extend your Worker class from ListenableWorker.

see:https://developer.android.com/topic/libraries/architecture/workmanager/basics#create_a_background_task

see:https://stackoverflow.com/a/52825547/6944302

Perihan Mirkelam
  • 526
  • 1
  • 5
  • 18
0

To download files Android has released latest api https://developer.android.com/reference/android/app/DownloadManager

Roman
  • 2,464
  • 2
  • 17
  • 21
0

Use Kotlin Coroutines to handle asynchronous operations. Example:

suspend fun download(): InputStream = withContext(Dispatchers.IO) {
    // a download function that returns a byte stream
}
92AlanC
  • 1,327
  • 2
  • 14
  • 33
0

DonwloadMabager with a Broadcast receiver is the safer.

james04
  • 1,580
  • 2
  • 20
  • 46