Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know whether a JobScheduler is running?

Tags:

android

After I invoke Code A, the JobScheduler will keep running even if I close the APP.

But some customize Android system maybe clear JobScheduler when close the APP.

How can check if the JobScheduler is running programmatically?

Code A

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

   val interval=if (isDebug())
                    10*1000L
                 else
                    mContext.getInteger(R.integer.AutoRestoreInterval)*60*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)
}

Code B

private fun stopScheduleRestore(mContext:Context){
    logError("Stop Server")

    val mJobScheduler = mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
    mJobScheduler.cancel(mContext.getInteger(R.integer.JobID))

}
like image 478
HelloCW Avatar asked May 23 '18 08:05

HelloCW


People also ask

How do I test my JobScheduler?

You can do this by using the adb shell cmd jobscheduler run command, (requires Android 7.1 or higher). Don't forget the -f option as this forces the job to run even if the restrictions set are not met.

What is JobScheduler explain how it works?

android.app.job.JobScheduler. This is an API for scheduling various types of jobs against the framework that will be executed in your application's own process. See JobInfo for more description of the types of jobs that can be run and how to construct them.

What is JobScheduler in Android?

JobScheduler is a system service that is used to schedule, execute, and if necessary cancel, jobs on behalf of an Android application. An Android. App. Job. JobService is an abstract class that must be extended with the logic that will run the job on the main thread of the application.

What is the use of JobScheduler?

The job scheduler API. The Android 5.0 Lollipop (API 21) release introduces a job scheduler API via the JobScheduler class. This API allows to batch jobs when the device has more resources available. In general this API can be used to schedule everything that is not time critical for the user.


2 Answers

You can use getAllPendingJobs method of JobScheduler. Based on documentation:

a list of all of the app's scheduled jobs. This includes jobs that are currently started as well as those that are still waiting to run.

public static boolean isJobSchedulerRunning(final Context context) {
    final JobScheduler jobScheduler = (JobScheduler) context.getSystemService( Context.JOB_SCHEDULER_SERVICE );    
    return jobScheduler.getAllPendingJobs().size() > 0;
}

If want to check if particular JobId is still valid then you can do following:

public static boolean isJobIdRunning( Context context, int JobId) {
    final JobScheduler jobScheduler = (JobScheduler) context.getSystemService( Context.JOB_SCHEDULER_SERVICE ) ;

    for ( JobInfo jobInfo : jobScheduler.getAllPendingJobs() ) {
        if ( jobInfo.getId() == JobId ) {
           return true;
        }
    }

    return false;
}
like image 189
Sagar Avatar answered Nov 14 '22 20:11

Sagar


Run Adb Command

adb shell dumpsys jobscheduler

It will show you list of jobs scheduled and you can identify you own task

like image 21
Kapil Avatar answered Nov 14 '22 20:11

Kapil