Android JobScheduler JobService not working

2019-07-29 06:24发布

问题:

Hi I have implemented Android JobScheduler API which was working fine till recently but since 2/3 days there seems to be a inconsistent behaviour in jobs which are scheduled.

Here is the jobinfo builder config :

jobInfo = new JobInfo.Builder(118, new ComponentName(this, LocationJob.class)) .setBackoffCriteria(30000, JobInfo.BACKOFF_POLICY_EXPONENTIAL) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) .setExtras(persistableBundle).build();

jobScheduler.schedule(jobInfo);

Basic working is when i trigger this it used to start the Job Service class immediately but it ain't starting immediately now.

My JobService class :

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class LocationJob extends JobService {

    private static final String LOG_TAG = LocationJob.class.getSimpleName();

@Override
public boolean onStartJob(final JobParameters params) {

    try {
        /* doing some server work in another thread. */
    } catch (Exception e) {
        Log.v(LOG_TAG, e.toString() + " " + e.getMessage() + " " + e
                .getCause());
    }

    return true;
}

@Override
public boolean onStopJob(JobParameters params) {
    return false;
}
}

My JobService is not triggering immediately what sort of behaviour is this?

回答1:

That's normal behavior. It starts it "somewhere in the (near) future". It does the same in my app. Sometimes right away, not always.

Also, your requirements, like NETWORK_TYPE_ANY, could postpone it when there is no network at the moment.

From the documentation: "Typically if you don't specify a deadline on your job, it can be run at any moment depending on the current state of the JobScheduler's internal queue, however it might be deferred as long as until the next time the device is connected to a power source." https://developer.android.com/reference/android/app/job/JobScheduler.html