androidkotlinbroadcastreceiverkotlin-coroutinesjobintentservice

sendBroadcast from inside Service which uses Coroutine for network call


I have a JobIntentService which is supposed to do an API call and do a broadcast once the result is available.

I am using a Coroutine to do the network call using Retrofit. However, if I do sendBroadcast within the CoroutineScope , it does not trigger the BroadcastReceiver

This is my service code -

MyService.kt

class MyService : JobIntentService() {

    private val TAG = MyService::class.java.simpleName
    private var databaseHelper: DatabaseHelper = DatabaseHelper(this)
    private var imageFetcher: ImageFetcher = ImageFetcher(this)
    private var imageSaver: ImageSaver = ImageSaver(this)
    private val receiver = ServiceBroadcastReceiver()


    override fun onHandleWork(intent: Intent) {
        val filter = IntentFilter()
        filter.addAction("ACTION_FINISHED_SERVICE")
        registerReceiver(receiver, filter)

        when (intent.action) {
            "ACTION_FETCH_FROM_API" -> {
                handleFetchFromAPI()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(receiver)
    }

    private fun handleFetchFromAPI() {
        val API = ServiceBuilder.buildWebService(WebService::class.java)
        CoroutineScope(IO).launch {
            try {
                var apiSuccess : Boolean = false
                val apiResponse = API.getImageOfTheDay()
                if (apiResponse.isSuccessful) {
                    apiSuccess = true
                    val imageAPIResponse = apiResponse.body()
                    val bitmap = imageFetcher.getImageBitmapFromURL(imageAPIResponse.url)
                    val filePath = imageSaver.saveBitmapToFile(bitmap, "image.jpg")
                    withContext(Main) {
                        databaseHelper.saveImageInRoom(imageAPIResponse, filePath)
                    }
                }
                if(apiSuccess){
                    val broadCastIntent = Intent()
                    broadCastIntent.action = "ACTION_FINISHED_SERVICE"
                    sendBroadcast(broadCastIntent)
                } 
            } catch (exception: Exception) {
                Log.d(TAG, "Exception occurred ${exception.message}")
            }
        }
    }

    companion object {
        private const val JOB_ID = 2
        @JvmStatic
        fun enqueueWork(context: Context, intent: Intent) {
            enqueueWork(context, MyService::class.java, JOB_ID, intent)
        }
    }
}

ServiceBroadcastReceiver.kt class ServiceBroadcastReceiver : BroadcastReceiver() {

private val TAG = ServiceBroadcastReceiver::class.java.simpleName
private lateinit var _mNotificationManager: NotificationManager
private val _notificationId = 0
private val _primaryChannelId = "primary_notification_channel"

override fun onReceive(context: Context, intent: Intent) {
    _mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    when (intent.action) {
        "ACTION_FINISHED_SERVICE" -> {
            deliverNotification(context)
        }
    }
}

private fun deliverNotification(context: Context) {
    val contentIntent = Intent(context, MainActivity::class.java)
    val pendingIntent = PendingIntent.getActivity(context,_notificationId,contentIntent,
            PendingIntent.FLAG_UPDATE_CURRENT)
    val builder = NotificationCompat.Builder(context,_primaryChannelId)
    builder.setSmallIcon(R.mipmap.ic_launcher)
    builder.setContentTitle("Hi There")
    builder.setContentText("Service finished its job")
    builder.setContentIntent(pendingIntent)
    builder.priority = NotificationCompat.PRIORITY_HIGH
    builder.setAutoCancel(true)
    builder.setDefaults(NotificationCompat.DEFAULT_ALL)
    _mNotificationManager.notify(_notificationId,builder.build())
}

}

The getImageOfTheDay() is a suspend function inside WebService.kt

@Headers("Content-Type: application/json")
@GET("/v1/getImageOfTheDay")
suspend fun getImageOfTheDay(): Response<ImageAPIResponse>

If I move the code to outside the Coroutine scope, the broadcast is sent correctly. How can I fix this problem ?


Solution

  • You should not use a coroutine here. The onHandleWork method is called on a background thread, and returning from this method signals the work is done and the service can be terminated.

    As you are launching a coroutine with launch, the onHandleWork returns immediately and your service terminates.

    You should call your network API directly and not in a coroutine because JobIntentService is designed to work this way already.