Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Use IntentService

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

Build an IntentService and a JobIntentService, to represent one-off work, which you run and forget about.

Contributors

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

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

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)

Reviews

Comments