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 - Communication Between Components

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

Repeat everything you’ve learned about Services and BroadcastReceivers, in a fun challenge!

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.

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)

Reviews

Comments