Android Background Processing

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

Part 3: Use Android Services

17. Use IntentService

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: 16. Use Android Service Next episode: 18. Challenge - Services

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: 17. Use IntentService

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

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

The next type of service you’ll learn to implement serves as a one-off job. Similar to how you used the WorkManager, to start a piece of work, and then return a result, or not care about the result at all, you can do the same with a service.

const val SERVICE_NAME = "Download Image Service"

class DownloadService : IntentService(SERVICE_NAME) {

  override fun onHandleIntent(intent: Intent?) {
    val imagePath = intent?.getStringExtra("image_path")
    if (imagePath != null) {
      downloadImage(imagePath)
    }
  }

  private fun downloadImage(imagePath: String) {
    val file = File(applicationContext.externalMediaDirs.first(), imagePath)

    FileUtils.downloadImage(file, imagePath)
  }
}
class DownloadService : JobIntentService() {

  companion object {
    private const val JOB_ID = 10

    fun startWork(context: Context, intent: Intent) {
      enqueueWork(context, DownloadService::class.java, JOB_ID, intent)
    }
  }
  
  ...
}
class DownloadService : JobIntentService() {

  ...

  override fun onHandleWork(intent: Intent) {
    val imagePath = intent.getStringExtra("image_path")
    if (imagePath != null) {
      downloadImage(imagePath)
    } else {
      Log.d("Missing image path", "Stopping service")
      stopSelf()
    }
  }
  
  ...
}
class DownloadService : JobIntentService() {

  companion object {
    private const val JOB_ID = 10

    fun startWork(context: Context, intent: Intent) {
      enqueueWork(context, DownloadService::class.java, JOB_ID, intent)
    }
  }

  override fun onHandleWork(intent: Intent) {
    val imagePath = intent.getStringExtra("image_path")
    if (imagePath != null) {
      downloadImage(imagePath)
    } else {
      Log.d("Missing image path", "Stopping service")
      stopSelf()
    }
  }

  private fun downloadImage(imagePath: String) {
    val file = File(applicationContext.externalMediaDirs.first(), imagePath)

    FileUtils.downloadImage(file, imagePath)
  }
}
<uses-permission android:name="android.permission.WAKE_LOCK" />
<service
  android:name=".service.DownloadService"
  android:permission="android.permission.BIND_JOB_SERVICE" />
val intent = Intent()
intent.putExtra("image_path", imageUrl)
DownloadService.startWork(requireContext(), intent)