An overview of
JobScheduler is a task execution framework provided by the Android system. Through JobScheduler, we can schedule tasks based on different policies.
Such as:
- The specified task can be executed on the specified network
- Specified tasks can only be performed while charging
- Set the deadline of the task
- Specifies a delay for executing a task
- Specify tasks to be performed when the phone is idle
If the app is killed, the task cannot be executed. If you need to continue executing the task after restarting, it is recommended to use WorkManager
use
The basic use
Create a customized JobService and configure it in the manifest
class ExeTaskAfter2SecondService: JobService() {
override fun onStartJob(params: JobParameters?).: Boolean {
logEE("Five-second delay: onStartJob")
return true
}
override fun onStopJob(params: JobParameters?).: Boolean {
logEE("Five second delay: onStopJob")
return true}}Copy the code
<service android:name=".service.ExeTaskAfter2SecondService"
android:permission="android.permission.BIND_JOB_SERVICE"
/>
Copy the code
Scheduling Task Execution
val mJobScheduler = getSystemService(JOB_SCHEDULER_SERVICE) as JobScheduler// Obtain system services
val mBuilder = JobInfo.Builder(1, ComponentName(this, ExeTaskAfter2SecondService::class.java))//Builder the first parameter is the Job id, which can be used to cancel the task. The second parameter is the Service to execute the task logic
var mJobInfo = mBuilder
.setMinimumLatency(5000)// Execute the task after 5 seconds
.build()
mJobScheduler.schedule(mJobInfo)// The task is scheduled
Copy the code
Canceling a Task
mJobScheduler.cancel(1)// This id is the task ID specified in schedule
mJobScheduler.cancelAll()// Cancel all tasks
Copy the code
Set the deadline of the task
var mJobInfo = mBuilder
.setOverrideDeadline(5000)// It must be executed once within five seconds
.build()
mJobScheduler.schedule(mJobInfo)
Copy the code
Set the network status of the task
Only on mobile networks
If we start the task without a wireless connection, we do not perform the task, and then we automatically start the network when we connect the wireless network
val mBuilder = JobInfo.Builder(
1,
ComponentName(this, ExeTaskAfter2SecondService::class.java)
)//Builder the first parameter is the Job id, which can be used to cancel the task. The second parameter is the Service to execute the task logic
var mJobInfo = mBuilder
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_CELLULAR)
.build()
mJobScheduler.schedule(mJobInfo)// The task is scheduled
Copy the code
Specify the limit of network traffic when a job is executed
We don’t think about traffic, so I didn’t test it
setEstimatedNetworkBytes (long downloadBytes, long uploadBytes)
Copy the code
Run when the power is not low
setRequiresBatteryNotLow
Copy the code
You must have a network connection to perform this operation
NETWORK_TYPE_ANY
Can be executed regardless of network availability
NETWORK_TYPE_NONE
This command can be executed only with wifi
NETWORK_TYPE_UNMETERED
Specify tasks to be performed when the phone is idle
setRequiresDeviceIdle
It’s hard to predict
val mBuilder = JobInfo.Builder(
1,
ComponentName(this, ExeTaskAfter2SecondService::class.java)
)//Builder the first parameter is the Job id, which can be used to cancel the task. The second parameter is the Service to execute the task logic
var mJobInfo = mBuilder
SetOverrideDeadline (5000)// If the dealline time is set, setRequiresDeviceIdle will be executed even when the deadline is set
.setRequiresDeviceIdle(true)
.build()
mJobScheduler.schedule(mJobInfo)// The task is scheduled
Copy the code
This command can be executed only when the charging status is set
If we’re on a mission and the phone isn’t charged, the mission will start automatically when we plug it in
mJobScheduler = getSystemService(JOB_SCHEDULER_SERVICE) as JobScheduler
val mBuilder = JobInfo.Builder(
1,
ComponentName(this, ExeTaskAfter2SecondService::class.java)
)//Builder the first parameter is the Job id, which can be used to cancel the task. The second parameter is the Service to execute the task logic
var mJobInfo = mBuilder
.setRequiresCharging(true)// Set to charge
.build()
mJobScheduler.schedule(mJobInfo)// The task is scheduled
Copy the code
The API is introduced
JobService.onStartJob
The onStartJob method is called when jobScheduler.schedule is called
The return value of the onStartJob method:
If false is returned, the task ends when the method completes, and the onStopJob method is not called later (even if we call cancel actively).
The method can be executed repeatedly if true is returned
JobService.onStopJob
Calling jobschdeuler. cancel triggers the onStopJob callback
JobService.jobFinished
jobFinished(params,false) : the first parameter JobParameters, the second parameter indicates whether to repeat the executionCopy the code
The active notification system has completed its task
Git:github.com/ananananzhu…