6
votes

I try to setup JobService using FirebaseJobDispatcher.

Here is my JobService:

class MyJobService : JobService() {
override fun onStartJob(job: JobParameters): Boolean {
    // Do some work here
   return false //return false if job done otherwise return true
}

override fun onStopJob(job: JobParameters): Boolean {
    return false //Should this job be retried?"
   }
}

However when i try to setup it like this:

val dispatcher = FirebaseJobDispatcher(GooglePlayDriver(this))
val myJob = dispatcher.newJobBuilder()
    .setService(MyJobService::class.java) // the JobService that will be called
    .setTag("my-unique-tag")        // uniquely identifies the job
    .build()

I am getting this compiler error in Android Studio:

Type inference failed. Expected type mismatch: inferred type is Class but Class! was expected

How to setup it in Kotlin correctly?

1
You should try to .setService(MyJobService::class) or .setService(MyJobService::class.java!!) (to be compliant with Class!) I guess.MajorShepard
I've tried both before - not workedK.Os
Are you using the latest version ? firebase-jobdispatcher:0.8.5' ? I'm seeing Improve Kotlin support (#193) in their repository. Your code seems to be correct based on what I've read.MajorShepard
Yes, I am using exactly that version. I've created an issue also: github.com/firebase/firebase-jobdispatcher-android/issues/231K.Os
Hey I am facing same issue ! any updates on this ?Umar Farooque

1 Answers

3
votes

It seems that I imported wrong JobService.

Instead of:

import android.app.job.JobParameters
import android.app.job.JobService

Should be:

import com.firebase.jobdispatcher.JobParameters
import com.firebase.jobdispatcher.JobService