public final class JobManager
extends java.lang.Object
JobScheduler is preferred, if the OS is running Lollipop or above.
Otherwise it uses the AlarmManager as fallback. It's also possible to use the
GcmNetworkManager, if the manager can be found in your classpath, the Google Play Services
are installed and the service was added in the manifest. Take a look at the
README for more
help.
JobManager you must call create(Context) first and add a
JobCreator to map tags to your desired jobs with addJobCreator(JobCreator).
It's recommended to do this in the Application.onCreate() method.
Job class. A Job is a simple class
with one abstract method which is invoked on a background thread. Thus a Job
doesn't need to be registered in the manifest. Create a JobRequest with the corresponding
JobRequest.Builder, set your desired parameters and call schedule(JobRequest). If you want
to update a pending request, call JobRequest.cancelAndEdit() on the request, update your
parameters and call schedule(JobRequest) again.| Modifier and Type | Class and Description |
|---|---|
class |
JobManager.Config |
| Modifier and Type | Method and Description |
|---|---|
void |
addJobCreator(JobCreator jobCreator)
Registers this instance to create jobs for a specific tag.
|
boolean |
cancel(int jobId)
Cancel either the pending
JobRequest or the running Job. |
int |
cancelAll()
Cancel all pending JobRequests and running jobs.
|
int |
cancelAllForTag(java.lang.String tag)
Cancel all pending JobRequests and running jobs.
|
static JobManager |
create(android.content.Context context)
Initializes the singleton.
|
static JobManager |
create(android.content.Context context,
JobCreator jobCreator)
Deprecated.
Use
create(Context) instead and call addJobCreator(JobCreator) after that. |
void |
forceApi(JobApi api)
WARNING: You shouldn't call this method.
|
java.util.Set<JobRequest> |
getAllJobRequests() |
java.util.Set<JobRequest> |
getAllJobRequestsForTag(java.lang.String tag) |
java.util.Set<Job> |
getAllJobs()
Jobs are cached in memory even if they already have finished.
|
java.util.Set<Job> |
getAllJobsForTag(java.lang.String tag)
Jobs are cached in memory even if they already have finished.
|
JobApi |
getApi()
WARNING: Don't rely your logic on a specific
JobApi. |
JobManager.Config |
getConfig() |
Job |
getJob(int jobId)
Jobs are cached in memory even if they already have finished.
|
JobRequest |
getJobRequest(int jobId) |
static JobManager |
instance()
Ensure that you've called
create(Context) first. |
void |
removeJobCreator(JobCreator jobCreator)
Remove the mapping to stop it from creating new jobs.
|
void |
schedule(JobRequest request)
Schedule a request which will be executed in the future.
|
protected void |
setJobProxy(JobApi api) |
void |
setVerbose(boolean verbose)
Deprecated.
Use
JobManager.Config.setVerbose(boolean) instead. |
public static JobManager create(android.content.Context context)
JobManager.
Calling it multiple times has not effect.context - Any Context to instantiate the singleton object.@Deprecated public static JobManager create(android.content.Context context, JobCreator jobCreator)
create(Context) instead and call addJobCreator(JobCreator) after that.JobManager.
Calling it multiple times has not effect.context - Any Context to instantiate the singleton object.jobCreator - The mapping between a specific job tag and the job class.public static JobManager instance()
create(Context) first. Otherwise this method
throws an exception.JobManager object.public JobManager.Config getConfig()
protected void setJobProxy(JobApi api)
public void schedule(JobRequest request)
JobRequest, call JobRequest.cancelAndEdit(), update your parameters and call
this method again. Note that after a JobRequest was updated, it has a new unique ID.
JobRequestrequest - The JobRequest which will be run in the future.public JobRequest getJobRequest(int jobId)
jobId - The unique ID of the pending JobRequest.JobRequest if it's pending or null otherwise.public java.util.Set<JobRequest> getAllJobRequests()
Set containing all pending JobRequests or an empty set.
Never returns null. The set may be modified without direct effects to the actual
backing store.getJobRequest(int)public java.util.Set<JobRequest> getAllJobRequestsForTag(java.lang.String tag)
tag - The tag of the pending requests.Set containing all pending JobRequests associated with this
tag or an empty set. Never returns null. The set may be modified without
direct effects to the actual backing store.public Job getJob(int jobId)
public java.util.Set<Job> getAllJobs()
Set containing all running and cached finished jobs or an empty set.
Never returns null. The set may be modified without direct effects to the actual
backing store.public java.util.Set<Job> getAllJobsForTag(java.lang.String tag)
tag - The tag of the running or finished jobs.Set containing all running and cached finished jobs associated with
this tag or an empty set. Never returns null. The set may be modified without direct
effects to the actual backing store.public void forceApi(JobApi api)
JobManager automatically decides which API suits best for a Job.api - The JobApi which will be used for future scheduled JobRequests.public JobApi getApi()
JobApi. You shouldn't be worrying
about it.JobApi which will be used for future schedules JobRequests.public boolean cancel(int jobId)
JobRequest or the running Job.jobId - The unique ID of the JobRequest or running Job.true if a request or job were found and canceled.public int cancelAll()
public int cancelAllForTag(java.lang.String tag)
tag - The tag of the pending job requests and running jobs.@Deprecated public void setVerbose(boolean verbose)
JobManager.Config.setVerbose(boolean) instead.verbose - Whether or not to print log messages.public void addJobCreator(JobCreator jobCreator)
JobCreators with a first come first serve order.jobCreator - The mapping between a specific job tag and the job class.public void removeJobCreator(JobCreator jobCreator)
jobCreator - The mapping between a specific job tag and the job class.