HelloCW
HelloCW

Reputation: 2255

Why is the function onCreate() in JobService() always launched in Android?

In my mind, the JobService() is subclass of service class, it will be resident in system for a long time.

onCreate() will be invoked only one time and onStartJob will be invoked periodically.

So I think that the result will be

Start Server
OnCreate
Starting
Starting
Starting

But in fact, onCreate() is always launched before onStartJob(), you can see the Image ,why?

It seems that the system release RestoreService : JobService() quickly, but why doesn't onStopJob() to be invoked when the system release RestoreService : JobService()?

Image

enter image description here

Code

private fun startScheduleRestore(mContext:Context){
   logError("Start Server")

   val interval=10 *1000L

    val mJobScheduler = mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler

    val jobInfo = JobInfo.Builder(mContext.getInteger(R.integer.JobID), ComponentName(mContext, RestoreService::class.java))
                        .setPeriodic(interval)
                        .setPersisted(true)
                        .build()

    mJobScheduler.schedule(jobInfo)
}



class RestoreService : JobService() {
    override fun onCreate() {
        logError("OnCreate")
        super.onCreate()
    }

    override fun onDestroy() {
        logError("OnDestory")
        super.onDestroy()
    }

    override fun onStartJob(params: JobParameters): Boolean {
        Thread(Runnable { completeRestore(params) }).start()
        return true
    }

    override fun onStopJob(params: JobParameters): Boolean {
        logError("OnStop")
        return false
    }


    fun completeRestore(parameters: JobParameters) {
        logError("Starting")
        jobFinished(parameters, false)
    }

}

Upvotes: 1

Views: 1020

Answers (2)

Sagar
Sagar

Reputation: 24907

This is inline with the lifecycle of service defined by Google. onCreate will be called only once throughout the life-cycle of JobService.

Called by the system when the service is first created.

Since you are calling jobFinished in completeRestore your RestoreServicewill be destroyed and your next schedule will result in creating new RestoreService. This in turn will call onCreate

Upvotes: 1

Bhavesh Patadiya
Bhavesh Patadiya

Reputation: 25830

But in fact, onCreate() is always launched before onStartJob(), you can see the Image, why?

This is because according to your business logic the RestoreService is starting, doing the important things you provided in onStartJob() method and once done will destroy when you call jobFinished() method.

It seems that the system release RestoreService : JobService() quickly, but why doesn't onStopJob() to be invoked when the system release RestoreService : JobService()?

onStopJob() method is called if the system has determined that you must stop the execution of your job even before you've had a chance to call jobFinished(JobParameters, boolean).

Upvotes: 1

Related Questions