Skip to content

Version 1.2.0

Ralf Wondratschek edited this page Oct 5, 2017 · 1 revision

Version 1.2.0 supports all features from Android O and adds compatibility for the new background execution limits.

New features

Transient Bundle

The new version will support the transient Bundle with all APIs. It's not guaranteed that a transient Job will run, but if the time frame until the job starts is short, then you can use this feature to pass in a real Bundle.

Bundle bundle = new Bundle();
bundle.putString("key", "value");

new JobRequest.Builder("tag")
        .setExecutionWindow(40_000L, 50_000L)
        .setTransientExtras(bundle)
        .build()
        .schedule();

Start jobs immediately

A new startNow() method was added to start jobs immediately. Such a job is similar to an exact job, e.g. there are no additional parameters to specify. Compared to implementing your own Service class, startNow() is simpler to setup, since the acquisition of a wake lock is automatic and the job can also be rescheduled. The transient bundle makes a lot of sense in this implementation.

new JobRequest.Builder("tag")
        .startNow()
        .setBackoffCriteria(4_000, JobRequest.BackoffPolicy.LINEAR)
        .build()
        .schedule();

New job requirements

The new network type METERED is supported with all API levels. You are also now able to require "sufficient battery" and/or "sufficient storage" before a Job can be run.

new JobRequest.Builder("tag")
        .setExecutionWindow(40_000L, 50_000L)
        .setRequiredNetworkType(JobRequest.NetworkType.METERED)
        .setRequiresStorageNotLow(true)
        .setRequiresBatteryNotLow(true)
        .build()
        .schedule();

Add helper class to run jobs daily

A common scenario is to run a job once day. This couldn't be implemented easily with a periodic job, because the the first period needs to be delayed and the interval shouldn't shift. The new helper class DailyJob should ease the setting up of such jobs.

public class DailySyncJob extends DailyJob {

    public static final String TAG = "DailySyncJob";

    public static void schedule() {
        if (!JobManager.instance().getAllJobRequestsForTag(TAG).isEmpty()) {
            // job already scheduled, nothing to do
            return;
        }
        
        JobRequest.Builder builder = new JobRequest.Builder(TAG).setRequiredNetworkType(JobRequest.NetworkType.UNMETERED);
        
        // run job between 11pm and 6am
        DailyJob.schedule(builder, TimeUnit.HOURS.toMillis(23), TimeUnit.HOURS.toMillis(6));
    }

    @NonNull
    @Override
    protected DailyJobResult onRunDailyJob(Params params) {
        // do something
        return DailyJobResult.SUCCESS;
    }
}

Custom logger

You can use the JobConfig class to add a custom logger and to disable logging.

// disable logcat
JobConfig.setLogcatEnabled(false);

// custom logger
JobConfig.addLogger(new JobLogger() {
    @Override
    public void log(int priority, @NonNull String tag, @NonNull String message, @Nullable Throwable t) {
        Log.println(priority, tag, message);
    }
});

Breaking API changes

Only minor changes were made and shouldn't affect most apps.

  • Deprecated methods were removed.
  • Remove setPersisted() method. This didn't work reliably and jobs were persisted nonetheless. The library will run your job in all cases and reschedule them when necessary. The only exception are transient jobs (see above). So a job is either persisted by default or transient - a job is considered to be transient if you pass in a transient Bundle.
  • The JobManager.Config class was renamed to JobConfig to make it possible to change settings before the JobManager is created. Most apps don't need to make any changes. This is only useful for testing purposes.
  • Remove startWakefulService() from the Job class. This isn't needed anymore with the new JobIntentService class from the support library.
Clone this wiki locally