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.

Challenge - Services

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

Practice what you’ve learned about Services, to implement another JobIntentService, and run one-off work.

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.

It’s time to practice what you’ve learned about Services! :] In this challenge, you have to do one thing - implement the Synchronization feature using a one-off service.

class SynchronizeImagesService : JobIntentService() {

  private val remoteApi by lazy { App.remoteApi }

  companion object {
    private const val JOB_ID = 15

    fun startWork(context: Context, intent: Intent) {
      enqueueWork(context, SynchronizeImagesService::class.java, JOB_ID, intent)
    }
  }
}
<service
  android:name=".service.SynchronizeImagesService"
  android:permission="android.permission.BIND_JOB_SERVICE" />
class SynchronizeImagesService : JobIntentService() {

  private val remoteApi by lazy { App.remoteApi }

  companion object {
    private const val JOB_ID = 15

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

  override fun onHandleWork(intent: Intent) {
    clearStorage()
    fetchImages()
  }
  
  ...
}
class SynchronizeImagesService : JobIntentService() {

  ...

  private fun fetchImages() {
    GlobalScope.launch {
      val result = remoteApi.getImages()

      if (result is Success) {
        val imagesArray = result.data.map { it.imagePath }.toTypedArray()

        FileUtils.queueImagesForDownload(applicationContext, imagesArray)
      }
    }
  }

  private fun clearStorage() {
    FileUtils.clearLocalStorage(applicationContext)
  }
}
private fun synchronizeImages() {
  SynchronizeImagesService.startWork(requireContext(), Intent())
}

Reviews

Comments