Android Background Processing

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

Part 3: Use Android Services

21. Challenge - Communication Between Components

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: 20. Communicate Using BroadcastReceivers Next episode: 22. Conclusion

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: 21. Challenge - Communication Between Components

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.

To really understand Services and BroadcastReceivers, and the communication between such components, you have to solve a small challenge! :]

const val ACTION_IMAGE_UPLOAD = "image_upload"

class UploadImageReceiver(
    private inline val onImageUploaded: (Boolean) -> Unit) : BroadcastReceiver() {

  override fun onReceive(context: Context?, intent: Intent?) {
    if (intent?.action == ACTION_IMAGE_UPLOAD) {
      val isUploaded = intent.getBooleanExtra("is_uploaded", false)

      onImageUploaded(isUploaded)
    }
  }
}
class UploadService : JobIntentService() {

  private val remoteApi by lazy { App.remoteApi }

}
<service
  android:name=".service.UploadService"
  android:permission="android.permission.BIND_JOB_SERVICE" />
class UploadService : JobIntentService() {

  private val remoteApi by lazy { App.remoteApi }

  companion object {
    private const val JOB_ID = 52

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

  ...
}
class UploadService : JobIntentService() {

  ...

  override fun onHandleWork(intent: Intent) {
    val filePath = intent.getStringExtra("image_path")
    if (filePath != null) {
      uploadImage(filePath)
    }
  }

  ...
}
class UploadService : JobIntentService() {

  ...
  private fun uploadImage(filePath: String) {
    GlobalScope.launch {
      val result = remoteApi.uploadImage(File(filePath))

      val intent = Intent()
      intent.putExtra("is_uploaded", result.message == "Success!")
      intent.action = ACTION_IMAGE_UPLOAD

      sendBroadcast(intent)
    }
  }
 ... 
}
  private val synchronizeImagesReceiver by lazy {
    SynchronizeImagesReceiver {
      toast("Images synchronized!")
    }
  }

  private val uploadImageReceiver by lazy {
    UploadImageReceiver { isUploaded ->
      toast(if (isUploaded) "Image uploaded!" else "Upload failed! :[")
    }
  }
    registerReceiver(synchronizeImagesReceiver, IntentFilter().apply {
      addAction(ACTION_IMAGES_SYNCHRONIZED)
    })

    registerReceiver(uploadImageReceiver, IntentFilter().apply {
      addAction(ACTION_IMAGE_UPLOAD)
    })
  override fun onStop() {
    ...
    unregisterReceiver(uploadImageReceiver)
    super.onStop()
  }
val intent = Intent().apply { putExtra("image_path", fileUri) }
UploadService.startWork(requireContext(), intent)