JobServicepublic abstract class JobService extends android.app.Service Entry point for the callback from the {@link android.app.job.JobScheduler}.
This is the base class that handles asynchronous requests that were previously scheduled. You
are responsible for overriding {@link JobService#onStartJob(JobParameters)}, which is where
you will implement your job logic.
This service executes each incoming job on a {@link android.os.Handler} running on your
application's main thread. This means that you must offload your execution logic to
another thread/handler/{@link android.os.AsyncTask} of your choosing. Not doing so will result
in blocking any future callbacks from the JobManager - specifically
{@link #onStopJob(android.app.job.JobParameters)}, which is meant to inform you that the
scheduling requirements are no longer being met. |
Fields Summary |
---|
private static final String | TAG | public static final String | PERMISSION_BINDJob services must be protected with this permission:
...
If a job service is declared in the manifest but not protected with this
permission, that service will be ignored by the OS. | private final int | MSG_EXECUTE_JOBIdentifier for a message that will result in a call to
{@link #onStartJob(android.app.job.JobParameters)}. | private final int | MSG_STOP_JOBMessage that will result in a call to {@link #onStopJob(android.app.job.JobParameters)}. | private final int | MSG_JOB_FINISHEDMessage that the client has completed execution of this job. | private final Object | mHandlerLockLock object for {@link #mHandler}. | JobHandler | mHandlerHandler we post jobs to. Responsible for calling into the client logic, and handling the
callback to the system. | IJobService | mBinderBinder for this service. |
Methods Summary |
---|
void | ensureHandler()
synchronized (mHandlerLock) {
if (mHandler == null) {
mHandler = new JobHandler(getMainLooper());
}
}
| public final void | jobFinished(JobParameters params, boolean needsReschedule)Callback to inform the JobManager you've finished executing. This can be called from any
thread, as it will ultimately be run on your application's main thread. When the system
receives this message it will release the wakelock being held.
You can specify post-execution behaviour to the scheduler here with
needsReschedule . This will apply a back-off timer to your job based on
the default, or what was set with
{@link android.app.job.JobInfo.Builder#setBackoffCriteria(long, int)}. The original
requirements are always honoured even for a backed-off job. Note that a job running in
idle mode will not be backed-off. Instead what will happen is the job will be re-added
to the queue and re-executed within a future idle maintenance window.
ensureHandler();
Message m = Message.obtain(mHandler, MSG_JOB_FINISHED, params);
m.arg2 = needsReschedule ? 1 : 0;
m.sendToTarget();
| public final android.os.IBinder | onBind(android.content.Intent intent)
return mBinder.asBinder();
| public abstract boolean | onStartJob(JobParameters params)Override this method with the callback logic for your job. Any such logic needs to be
performed on a separate thread, as this function is executed on your application's main
thread.
| public abstract boolean | onStopJob(JobParameters params)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 {@link #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
{@link android.app.job.JobInfo.Builder#setRequiredNetworkType(int)}, yet while your
job was executing the user toggled WiFi. Another example is if you had specified
{@link android.app.job.JobInfo.Builder#setRequiresDeviceIdle(boolean)}, and the phone left its
idle maintenance window. You are solely responsible for the behaviour of your application
upon receipt of this message; your app will likely start to misbehave if you ignore it. One
immediate repercussion is that the system will cease holding a wakelock for you.
|
|