javaandroidinheritanceandroid-workmanagerjobservice

How to fix "specify a valid range of job id's for WorkManager to use"


I've extended JobService as such:

public class MyJobService extends JobService {

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

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

The entire class code is highlighted in yellow.
The warning says:

Specify a valid range of job id's for WorkManager to use.

What am I doing wrong and what should I do?


Solution

  • The warning suggests, that as long as an implementation of JobService class is created then user should be specifying which IDs the WorkManager is eligible to use "in order to not clash job codes used in the rest of your app" (see dosc at setJobSchedulerJobIdRange(int, int)).

    This should be done upon creation of androidx.work.Configuration instance:

    Configuration.Builder builder = new Configuration.Builder();
    builder.setJobSchedulerJobIdRange(0, 1000);
    ...
    

    Unfortunately, I couldn't make the thing work for my setup and I had to look into the sources of lint (SpecifyJobSchedulerIdRangeIssueDetector). As I can understand from the logic, as soon as there is a method call to setJobSchedulerJobIdRange (see line 92) - then the detector will assume that job range id has been specified by the user, but that doesn't work (at least on my machine).

    Unfortunately, checking the test class for the detector didn't convey anything either.

    As for now I can claim that:

    Interestingly, calling setJobSchedulerJobIdRange() in the JobService class fixes the warning:

    enter image description here

    Apparently, there are some issues with this detector implementation (or on my machine).

    Nevertheless, if you've specified the range of IDs that WorkManager is free to use (i.e. you have called setJobSchedulerJobIdRange()) - then you're good to suppress this warning.