[JAVA] Your use of JobScheduler is wrong

What's wrong

In the main Japanese commentary articles, the way to call JobService.jobFinished (JobParameters, boolean) is almost wrong [^ 1]. It may be described as follows, but this is incorrect.

@override
public boolean onStopJob(JobParameters params) {
  jobFinished(params, false);
  return false;
}

Similarly, there is an article that calling jobFinished calls ʻonStopJob`, but that is also incorrect.

Read Javadoc

jobFinished is, according to Javadoc

Call this to inform the JobScheduler that the job has finished its work. When the system receives this message, it releases the wakelock being held for the job.

It is written. Translated, "Calling this method ** notifies JobScheduler that the job is complete **. When the system receives this notification, it releases the wakelock it was getting for this job." .. In other words, when jobFinished () is called, Android interprets that the registered job has finished executing and releases the lock.

ʻOnStopJob` is written in Javadoc like this.

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

This will happen if the requirements specified at schedule time are no longer met. For example you may have requested WiFi with JobInfo.Builder.setRequiredNetworkType(int), yet while your job was executing the user toggled WiFi. Another example is if you had specified JobInfo.Builder.setRequiresDeviceIdle(boolean), and the phone left its idle maintenance window.

In other words, "This method is called when you need to ** stop a running job ** before you get a chance to call jobFinished () (that is, before the job finishes running). Was requested to JobScheduler to call a job under a specific condition, so the job was called when the condition was met, but it occurs when the condition is violated during the execution. For example, if you asked JobScheduler to call a job when connecting to WiFi, the job was called because it became a WiFi connection, but the WiFi connection was cut off while the job was running. ". In other words, the expected value when this method is called is "** Stop the running job " and " Notify that the job is completed ** (Call jobFinished ". That) ”is not.

Correct processing

Stop the job when ʻonStopJob ()` is called.

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

You don't have to worry about wakelock because it will be released when you return it with the onStopJob method.

Once this method returns, the system releases the wakelock that it is holding on behalf of the job.

What is google's sample code?

If you look at googlesamples,

    @Override
    public boolean onStopJob(JobParameters params) {
        // Stop tracking these job parameters, as we've 'finished' executing.
        sendMessage(MSG_COLOR_STOP, params.getJobId());
        Log.i(TAG, "on stop job: " + params.getJobId());

        // Return false to drop the job.
        return false;
    }

Processing is stopped by sending the message MSG_COLOR_STOP. Of course, I didn't call it jobFinished ().

[^ 1]: Searching for "Android JobScheduler onStopJob", 3 of the 4 Japanese articles that appear on the first page are incorrect. One is under-implemented. In other words, annihilation.

Recommended Posts

Your use of JobScheduler is wrong
Use of Date class
The essence of AspectJ's mood-why your `@Transactional` is ignored
Use @ValueSource of ParametrisedTest of JUnit5