As From android developer JobIntentService When running on Android O or later, the work will be dispatched as a job via JobScheduler.enqueue
. When running on older versions of the platform, it will use Context.startService
.
In my case i am learning JobIntentService
and in my case i have a timer that run every one second and display the current date and time but when my app get destroyed the JobIntentService
also get destroyed, How can i run it when app is destroyed
JobIntentService
class OreoService : JobIntentService() {
private val handler = Handler()
companion object {
private const val JOB_ID = 123
fun enqueueWork(cxt: Context, intent: Intent){
enqueueWork(cxt,OreoService::class.java,JOB_ID,intent)
}
}
override fun onHandleWork(intent: Intent) {
toast(intent.getStringExtra("val"))
Timer().scheduleAtFixedRate(object : TimerTask() {
override fun run() {
println(Date().toString())
}
}, Date(),1000)
}
override fun onDestroy() {
super.onDestroy()
toast("Service Destroyed")
}
private fun toast(msg: String){
handler.post({
Toast.makeText(applicationContext,msg,Toast.LENGTH_LONG).show()
})
}
}
Manifest
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
....... >
<service android:name=".service.OreoService"
android:permission="android.permission.BIND_JOB_SERVICE"/>
</application>
MainActivity (When button is pressed the service get started)
startServiceBtn.setOnClickListener({
val intent = Intent()
intent.putExtra("val","testing service")
OreoService.enqueueWork(this,intent)
})
IntentService has never been designed to be cancelled, hence the lack of any API to cancel a JobIntentService . Therefore, JobIntenService should only be used for work that don't need to be cancelled. Note that JobIntentService can still be used for work that can fail, and consequently, be aborted.
Use enqueueWork to enqueue new work to be dispatched to and handled by your service. It will be executed in onHandleWork .
Once his job is finished is automatically destroyed. In other words, you can say IntentService is advanced background service that creates a separate background thread to perform operations and automatically destroyed when the job is done.
i am learning JobIntentService and in my case i have a timer that run every one second and display the current date and time
That is not a suitable use for JobIntentService
(or pretty much anything else, for that matter).
when my app get destroyed the JobIntentService also get destroyed
The point of JobIntentService
is to do a little bit of work — some disk I/O, some network I/O, etc. — and then go away. It is not for doing something indefinitely, and it is not suitable for starting asynchronous work, and you are trying to do both. Once onHandleWork()
ends, the service goes away, and your process can be terminated at any point in time after that, which will stop your Timer
.
How can i run it when app is destroyed
You are welcome to use a foreground Service
, not an IntentService
or JobIntentService
. Return START_STICKY
from onStartCommand()
to ask Android to restart your service if your process is terminated (e.g., due to low memory conditions). Even that may be terminated by the user, but it is the user's device, not yours, and so the user can do whatever the user wants.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With