我尝试将 JobSchedulers 用于我的聊天应用程序(用于异步发送消息):
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)
并在清单中添加我的服务:
<service
android:exported="false"
android:name=".data.background.service.BackgroundJobService">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>
我写了这个 JobService,它没有调用 onCreate() 也没有调用 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()
}
}
但我的 onStartJob 没有打电话!我的错误在哪里?我看到消息“Google Play 服务已停止”。