Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JobIntentService get destroyed , When app is destroyed

Tags:

android

kotlin

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)
        })
like image 401
Salman500 Avatar asked Apr 21 '18 12:04

Salman500


People also ask

How do I stop JobIntentService?

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.

What task does the Enquework () function for the JobIntentService perform?

Use enqueueWork to enqueue new work to be dispatched to and handled by your service. It will be executed in onHandleWork .

How Intent service will close automatically once Job is done?

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.


1 Answers

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.

like image 96
CommonsWare Avatar answered Oct 13 '22 00:10

CommonsWare