1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package android.app.job; 18 19 import android.app.Service; 20 import android.content.Intent; 21 import android.os.IBinder; 22 23 /** 24 * <p>Entry point for the callback from the {@link android.app.job.JobScheduler}.</p> 25 * <p>This is the base class that handles asynchronous requests that were previously scheduled. You 26 * are responsible for overriding {@link JobService#onStartJob(JobParameters)}, which is where 27 * you will implement your job logic.</p> 28 * <p>This service executes each incoming job on a {@link android.os.Handler} running on your 29 * application's main thread. This means that you <b>must</b> offload your execution logic to 30 * another thread/handler/{@link android.os.AsyncTask} of your choosing. Not doing so will result 31 * in blocking any future callbacks from the JobManager - specifically 32 * {@link #onStopJob(android.app.job.JobParameters)}, which is meant to inform you that the 33 * scheduling requirements are no longer being met.</p> 34 */ 35 public abstract class JobService extends Service { 36 private static final String TAG = "JobService"; 37 38 /** 39 * Job services must be protected with this permission: 40 * 41 * <pre class="prettyprint"> 42 * <service android:name="MyJobService" 43 * android:permission="android.permission.BIND_JOB_SERVICE" > 44 * ... 45 * </service> 46 * </pre> 47 * 48 * <p>If a job service is declared in the manifest but not protected with this 49 * permission, that service will be ignored by the system. 50 */ 51 public static final String PERMISSION_BIND = 52 "android.permission.BIND_JOB_SERVICE"; 53 54 private JobServiceEngine mEngine; 55 56 /** @hide */ onBind(Intent intent)57 public final IBinder onBind(Intent intent) { 58 if (mEngine == null) { 59 mEngine = new JobServiceEngine(this) { 60 @Override 61 public boolean onStartJob(JobParameters params) { 62 return JobService.this.onStartJob(params); 63 } 64 65 @Override 66 public boolean onStopJob(JobParameters params) { 67 return JobService.this.onStopJob(params); 68 } 69 }; 70 } 71 return mEngine.getBinder(); 72 } 73 74 /** 75 * Call this to inform the JobScheduler that the job has finished its work. When the 76 * system receives this message, it releases the wakelock being held for the job. 77 * This does not need to be called if {@link #onStopJob(JobParameters)} has been called. 78 * <p> 79 * You can request that the job be scheduled again by passing {@code true} as 80 * the <code>wantsReschedule</code> parameter. This will apply back-off policy 81 * for the job; this policy can be adjusted through the 82 * {@link android.app.job.JobInfo.Builder#setBackoffCriteria(long, int)} method 83 * when the job is originally scheduled. The job's initial 84 * requirements are preserved when jobs are rescheduled, regardless of backed-off 85 * policy. 86 * <p class="note"> 87 * A job running while the device is dozing will not be rescheduled with the normal back-off 88 * policy. Instead, the job will be re-added to the queue and executed again during 89 * a future idle maintenance window. 90 * </p> 91 * 92 * @param params The parameters identifying this job, as supplied to 93 * the job in the {@link #onStartJob(JobParameters)} callback. 94 * @param wantsReschedule {@code true} if this job should be rescheduled according 95 * to the back-off criteria specified when it was first scheduled; {@code false} 96 * otherwise. 97 */ jobFinished(JobParameters params, boolean wantsReschedule)98 public final void jobFinished(JobParameters params, boolean wantsReschedule) { 99 mEngine.jobFinished(params, wantsReschedule); 100 } 101 102 /** 103 * Called to indicate that the job has begun executing. Override this method with the 104 * logic for your job. Like all other component lifecycle callbacks, this method executes 105 * on your application's main thread. 106 * <p> 107 * Return {@code true} from this method if your job needs to continue running. If you 108 * do this, the job remains active until you call 109 * {@link #jobFinished(JobParameters, boolean)} to tell the system that it has completed 110 * its work, or until the job's required constraints are no longer satisfied. For 111 * example, if the job was scheduled using 112 * {@link JobInfo.Builder#setRequiresCharging(boolean) setRequiresCharging(true)}, 113 * it will be immediately halted by the system if the user unplugs the device from power, 114 * the job's {@link #onStopJob(JobParameters)} callback will be invoked, and the app 115 * will be expected to shut down all ongoing work connected with that job. 116 * <p> 117 * The system holds a wakelock on behalf of your app as long as your job is executing. 118 * This wakelock is acquired before this method is invoked, and is not released until either 119 * you call {@link #jobFinished(JobParameters, boolean)}, or after the system invokes 120 * {@link #onStopJob(JobParameters)} to notify your job that it is being shut down 121 * prematurely. 122 * <p> 123 * Returning {@code false} from this method means your job is already finished. The 124 * system's wakelock for the job will be released, and {@link #onStopJob(JobParameters)} 125 * will not be invoked. 126 * 127 * @param params Parameters specifying info about this job, including the optional 128 * extras configured with {@link JobInfo.Builder#setExtras(android.os.PersistableBundle). 129 * This object serves to identify this specific running job instance when calling 130 * {@link #jobFinished(JobParameters, boolean)}. 131 * @return {@code true} if your service will continue running, using a separate thread 132 * when appropriate. {@code false} means that this job has completed its work. 133 */ onStartJob(JobParameters params)134 public abstract boolean onStartJob(JobParameters params); 135 136 /** 137 * This method is called if the system has determined that you must stop execution of your job 138 * even before you've had a chance to call {@link #jobFinished(JobParameters, boolean)}. 139 * Once this method is called, you no longer need to call 140 * {@link #jobFinished(JobParameters, boolean)}. 141 * 142 * <p>This may happen if the requirements specified at schedule time are no longer met. For 143 * example you may have requested WiFi with 144 * {@link android.app.job.JobInfo.Builder#setRequiredNetworkType(int)}, yet while your 145 * job was executing the user toggled WiFi. Another example is if you had specified 146 * {@link android.app.job.JobInfo.Builder#setRequiresDeviceIdle(boolean)}, and the phone left 147 * its idle maintenance window. There are many other reasons a job can be stopped early besides 148 * constraints no longer being satisfied. {@link JobParameters#getStopReason()} will return the 149 * reason this method was called. You are solely responsible for the behavior of your 150 * application upon receipt of this message; your app will likely start to misbehave if you 151 * ignore it. 152 * <p> 153 * Once this method returns (or times out), the system releases the wakelock that it is holding 154 * on behalf of the job.</p> 155 * 156 * <p class="caution"><strong>Note:</strong> When a job is stopped and rescheduled via this 157 * method call, the deadline constraint is excluded from the rescheduled job's constraint set. 158 * The rescheduled job will run again once all remaining constraints are satisfied. 159 * 160 * @param params The parameters identifying this job, similar to what was supplied to the job in 161 * the {@link #onStartJob(JobParameters)} callback, but with the stop reason 162 * included. 163 * @return {@code true} to indicate to the JobManager whether you'd like to reschedule 164 * this job based on the retry criteria provided at job creation-time; or {@code false} 165 * to end the job entirely. Regardless of the value returned, your job must stop executing. 166 */ onStopJob(JobParameters params)167 public abstract boolean onStopJob(JobParameters params); 168 } 169