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.Handler; 22 import android.os.IBinder; 23 import android.os.Looper; 24 import android.os.Message; 25 import android.os.RemoteException; 26 import android.util.Log; 27 28 import com.android.internal.annotations.GuardedBy; 29 30 import java.lang.ref.WeakReference; 31 32 /** 33 * <p>Entry point for the callback from the {@link android.app.job.JobScheduler}.</p> 34 * <p>This is the base class that handles asynchronous requests that were previously scheduled. You 35 * are responsible for overriding {@link JobService#onStartJob(JobParameters)}, which is where 36 * you will implement your job logic.</p> 37 * <p>This service executes each incoming job on a {@link android.os.Handler} running on your 38 * application's main thread. This means that you <b>must</b> offload your execution logic to 39 * another thread/handler/{@link android.os.AsyncTask} of your choosing. Not doing so will result 40 * in blocking any future callbacks from the JobManager - specifically 41 * {@link #onStopJob(android.app.job.JobParameters)}, which is meant to inform you that the 42 * scheduling requirements are no longer being met.</p> 43 */ 44 public abstract class JobService extends Service { 45 private static final String TAG = "JobService"; 46 47 /** 48 * Job services must be protected with this permission: 49 * 50 * <pre class="prettyprint"> 51 * <service android:name="MyJobService" 52 * android:permission="android.permission.BIND_JOB_SERVICE" > 53 * ... 54 * </service> 55 * </pre> 56 * 57 * <p>If a job service is declared in the manifest but not protected with this 58 * permission, that service will be ignored by the OS. 59 */ 60 public static final String PERMISSION_BIND = 61 "android.permission.BIND_JOB_SERVICE"; 62 63 private JobServiceEngine mEngine; 64 65 /** @hide */ onBind(Intent intent)66 public final IBinder onBind(Intent intent) { 67 if (mEngine == null) { 68 mEngine = new JobServiceEngine(this) { 69 @Override 70 public boolean onStartJob(JobParameters params) { 71 return JobService.this.onStartJob(params); 72 } 73 74 @Override 75 public boolean onStopJob(JobParameters params) { 76 return JobService.this.onStopJob(params); 77 } 78 }; 79 } 80 return mEngine.getBinder(); 81 } 82 83 /** 84 * Override this method with the callback logic for your job. Any such logic needs to be 85 * performed on a separate thread, as this function is executed on your application's main 86 * thread. 87 * 88 * @param params Parameters specifying info about this job, including the extras bundle you 89 * optionally provided at job-creation time. 90 * @return True if your service needs to process the work (on a separate thread). False if 91 * there's no more work to be done for this job. 92 */ onStartJob(JobParameters params)93 public abstract boolean onStartJob(JobParameters params); 94 95 /** 96 * This method is called if the system has determined that you must stop execution of your job 97 * even before you've had a chance to call {@link #jobFinished(JobParameters, boolean)}. 98 * 99 * <p>This will happen if the requirements specified at schedule time are no longer met. For 100 * example you may have requested WiFi with 101 * {@link android.app.job.JobInfo.Builder#setRequiredNetworkType(int)}, yet while your 102 * job was executing the user toggled WiFi. Another example is if you had specified 103 * {@link android.app.job.JobInfo.Builder#setRequiresDeviceIdle(boolean)}, and the phone left its 104 * idle maintenance window. You are solely responsible for the behaviour of your application 105 * upon receipt of this message; your app will likely start to misbehave if you ignore it. One 106 * immediate repercussion is that the system will cease holding a wakelock for you.</p> 107 * 108 * @param params Parameters specifying info about this job. 109 * @return True to indicate to the JobManager whether you'd like to reschedule this job based 110 * on the retry criteria provided at job creation-time. False to drop the job. Regardless of 111 * the value returned, your job must stop executing. 112 */ onStopJob(JobParameters params)113 public abstract boolean onStopJob(JobParameters params); 114 115 /** 116 * Call this to inform the JobManager you've finished executing. This can be called from any 117 * thread, as it will ultimately be run on your application's main thread. When the system 118 * receives this message it will release the wakelock being held. 119 * <p> 120 * You can specify post-execution behaviour to the scheduler here with 121 * <code>needsReschedule </code>. This will apply a back-off timer to your job based on 122 * the default, or what was set with 123 * {@link android.app.job.JobInfo.Builder#setBackoffCriteria(long, int)}. The original 124 * requirements are always honoured even for a backed-off job. Note that a job running in 125 * idle mode will not be backed-off. Instead what will happen is the job will be re-added 126 * to the queue and re-executed within a future idle maintenance window. 127 * </p> 128 * 129 * @param params Parameters specifying system-provided info about this job, this was given to 130 * your application in {@link #onStartJob(JobParameters)}. 131 * @param needsReschedule True if this job should be rescheduled according to the back-off 132 * criteria specified at schedule-time. False otherwise. 133 */ jobFinished(JobParameters params, boolean needsReschedule)134 public final void jobFinished(JobParameters params, boolean needsReschedule) { 135 mEngine.jobFinished(params, needsReschedule); 136 } 137 }