I try using JobSchedulers for my chat app (for async send message):
val job = dispatcher.newJobBuilder()
.setService(BackgroundJobService::class.java)
.setTag(BackgroundJobService.TASKTAG_SEND_MESSAGE)
.setReplaceCurrent(false)
.setConstraints(Constraint.ON_ANY_NETWORK)
.setExtras(bundle)
.setTrigger(Trigger.executionWindow(0, 1))
.build()
dispatcher.mustSchedule(job)
And add my service in manifest:
<service
android:exported="false"
android:name=".data.background.service.BackgroundJobService">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>
I write this JobService and it not called onCreate() and not called onDestroy()
class BackgroundJobService : JobService() {
companion object {
val TASKTAG_SEND_MESSAGE = "send_message"
}
@Inject
lateinit var webSocket: IRxWebSocket
var subscribe: Disposable? = null
override fun onCreate() {
super.onCreate()
App.appComponent.inject(this)
subscribe = webSocket.connect().subscribe({}, {})
}
override fun onStartJob(job: JobParameters?): Boolean {
return true // Debug point here
}
override fun onStopJob(job: JobParameters?): Boolean {
return true
}
override fun onDestroy() {
super.onDestroy()
subscribe?.dispose()
}
}
But my onStartJob not calling! Where is my mistake? Something i see message "Google Play services has stopped".
Your manifest declaration is wrong: you need to declare job services as requiring a specific permission that only the OS itself can use. From the JobService documentation:
Job services must be protected with this permission:
<service android:name="MyJobService" android:permission="android.permission.BIND_JOB_SERVICE" >
If a job service is declared in the manifest but not protected with this permission, that service will be ignored by the OS.