1 /* 2 * Copyright 2014 Google Inc. 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 com.example.android.jobscheduler.service; 18 19 import android.app.job.JobParameters; 20 import android.app.job.JobService; 21 import android.content.Intent; 22 import android.os.Handler; 23 import android.os.Message; 24 import android.os.Messenger; 25 import android.os.RemoteException; 26 import android.support.annotation.Nullable; 27 import android.util.Log; 28 29 30 import static com.example.android.jobscheduler.MainActivity.MESSENGER_INTENT_KEY; 31 import static com.example.android.jobscheduler.MainActivity.MSG_COLOR_START; 32 import static com.example.android.jobscheduler.MainActivity.MSG_COLOR_STOP; 33 import static com.example.android.jobscheduler.MainActivity.WORK_DURATION_KEY; 34 35 36 /** 37 * Service to handle callbacks from the JobScheduler. Requests scheduled with the JobScheduler 38 * ultimately land on this service's "onStartJob" method. It runs jobs for a specific amount of time 39 * and finishes them. It keeps the activity updated with changes via a Messenger. 40 */ 41 public class MyJobService extends JobService { 42 43 private static final String TAG = MyJobService.class.getSimpleName(); 44 45 private Messenger mActivityMessenger; 46 47 @Override 48 public void onCreate() { 49 super.onCreate(); 50 Log.i(TAG, "Service created"); 51 } 52 53 @Override 54 public void onDestroy() { 55 super.onDestroy(); 56 Log.i(TAG, "Service destroyed"); 57 } 58 59 /** 60 * When the app's MainActivity is created, it starts this service. This is so that the 61 * activity and this service can communicate back and forth. See "setUiCallback()" 62 */ 63 @Override 64 public int onStartCommand(Intent intent, int flags, int startId) { 65 mActivityMessenger = intent.getParcelableExtra(MESSENGER_INTENT_KEY); 66 return START_NOT_STICKY; 67 } 68 69 @Override 70 public boolean onStartJob(final JobParameters params) { 71 // The work that this service "does" is simply wait for a certain duration and finish 72 // the job (on another thread). 73 74 sendMessage(MSG_COLOR_START, params.getJobId()); 75 76 long duration = params.getExtras().getLong(WORK_DURATION_KEY); 77 78 // Uses a handler to delay the execution of jobFinished(). 79 Handler handler = new Handler(); 80 handler.postDelayed(new Runnable() { 81 @Override 82 public void run() { 83 sendMessage(MSG_COLOR_STOP, params.getJobId()); 84 jobFinished(params, false); 85 } 86 }, duration); 87 Log.i(TAG, "on start job: " + params.getJobId()); 88 89 // Return true as there's more work to be done with this job. 90 return true; 91 } 92 93 @Override 94 public boolean onStopJob(JobParameters params) { 95 // Stop tracking these job parameters, as we've 'finished' executing. 96 sendMessage(MSG_COLOR_STOP, params.getJobId()); 97 Log.i(TAG, "on stop job: " + params.getJobId()); 98 99 // Return false to drop the job. 100 return false; 101 } 102 103 private void sendMessage(int messageID, @Nullable Object params) { 104 // If this service is launched by the JobScheduler, there's no callback Messenger. It 105 // only exists when the MainActivity calls startService() with the callback in the Intent. 106 if (mActivityMessenger == null) { 107 Log.d(TAG, "Service is bound, not started. There's no callback to send a message to."); 108 return; 109 } 110 Message m = Message.obtain(); 111 m.what = messageID; 112 m.obj = params; 113 try { 114 mActivityMessenger.send(m); 115 } catch (RemoteException e) { 116 Log.e(TAG, "Error passing service object back to activity."); 117 } 118 } 119 } 120