-
Notifications
You must be signed in to change notification settings - Fork 559
Version 1.2.0
Version 1.2.0
supports all features from Android O and adds compatibility for the new background execution limits.
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();
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();
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();
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;
}
}
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);
}
});
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 transientBundle
. - The
JobManager.Config
class was renamed toJobConfig
to make it possible to change settings before theJobManager
is created. Most apps don't need to make any changes. This is only useful for testing purposes. - Remove
startWakefulService()
from theJob
class. This isn't needed anymore with the newJobIntentService
class from the support library.