public final class JobManager extends 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 | 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(String tag)
Cancel all pending JobRequests and running jobs.
|
static JobManager |
create(Context context)
Initializes the singleton.
|
(package private) void |
destroy() |
Set<JobRequest> |
getAllJobRequests() |
(package private) Set<JobRequest> |
getAllJobRequests(String tag,
boolean includeStarted,
boolean cleanUpTransient) |
Set<JobRequest> |
getAllJobRequestsForTag(String tag) |
SparseArray<Job.Result> |
getAllJobResults()
Finished jobs are kept in memory until the garbage collector cleans them up.
|
Set<Job> |
getAllJobs()
Jobs are cached in memory even if they already have finished.
|
Set<Job> |
getAllJobsForTag(String tag)
Jobs are cached in memory even if they already have finished.
|
(package private) Context |
getContext() |
Job |
getJob(int jobId)
Jobs are cached in memory even if they already have finished.
|
(package private) JobCreatorHolder |
getJobCreatorHolder() |
(package private) JobExecutor |
getJobExecutor() |
(package private) JobProxy |
getJobProxy(JobApi api) |
JobRequest |
getJobRequest(int jobId) |
(package private) JobRequest |
getJobRequest(int jobId,
boolean includeStarted) |
(package private) JobStorage |
getJobStorage() |
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.
|
public static JobManager create(@NonNull Context context) throws JobManagerCreateException
JobManager
.
Calling it multiple times has not effect.context
- Any Context
to instantiate the singleton object.JobManagerCreateException
- When the singleton couldn't be created.public static JobManager instance()
create(Context)
first. Otherwise this method
throws an exception.JobManager
object.public void schedule(@NonNull JobRequest request)
JobRequest
, call JobRequest.cancelAndEdit()
, update your parameters and call
this method again. Calling this method with the same request multiple times without cancelling
it is idempotent.request
- The JobRequest
which will 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.JobRequest getJobRequest(int jobId, boolean includeStarted)
@NonNull public 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 Set<JobRequest> getAllJobRequestsForTag(@NonNull 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.Set<JobRequest> getAllJobRequests(@Nullable String tag, boolean includeStarted, boolean cleanUpTransient)
public Job getJob(int jobId)
WeakReference
and can be removed from memory. If you need to know the results
of finished jobs or whether a job has been run, you can call getAllJobResults()
.@NonNull public Set<Job> getAllJobs()
WeakReference
and can be removed from memory. If you need to know the results
of finished jobs or whether a job has been run, you can call getAllJobResults()
.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.@NonNull public Set<Job> getAllJobsForTag(@NonNull String tag)
WeakReference
and can be removed from memory. If you need to know the results
of finished jobs or whether a job has been run, you can call getAllJobResults()
.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.@NonNull public SparseArray<Job.Result> getAllJobResults()
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(@NonNull String tag)
tag
- The tag of the pending job requests and running jobs.public void addJobCreator(JobCreator jobCreator)
JobCreator
s 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.JobStorage getJobStorage()
JobExecutor getJobExecutor()
JobCreatorHolder getJobCreatorHolder()
Context getContext()
void destroy()