TestJobService.java revision 7060b04f6d92351b67222e636ab378a0273bf3e7
1/* 2 * Copyright 2013 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 17package com.android.demo.jobSchedulerApp.service; 18 19import android.app.job.JobInfo; 20import android.app.job.JobScheduler; 21import android.app.job.JobParameters; 22import android.app.job.JobService; 23import android.content.Context; 24import android.content.Intent; 25import android.os.Message; 26import android.os.Messenger; 27import android.os.RemoteException; 28import android.util.Log; 29 30import com.android.demo.jobSchedulerApp.MainActivity; 31 32import java.util.LinkedList; 33 34 35/** 36 * Service to handle sync requests. 37 * <p> 38 * This service is invoked in response to Intents with action android.content.SyncAdapter, and 39 * returns a Binder connection to SyncAdapter. 40 * <p> 41 * For performance, only one sync adapter will be initialized within this application's context. 42 * <p> 43 * Note: The SyncService itself is not notified when a new sync occurs. It's role is to manage the 44 * lifecycle of our and provide a handle to said SyncAdapter to the OS on 45 * request. 46 */ 47public class TestJobService extends JobService { 48 private static final String TAG = "SyncService"; 49 50 @Override 51 public void onCreate() { 52 super.onCreate(); 53 Log.i(TAG, "Service created"); 54 } 55 56 @Override 57 public void onDestroy() { 58 super.onDestroy(); 59 Log.i(TAG, "Service destroyed"); 60 } 61 62 @Override 63 public int onStartCommand(Intent intent, int flags, int startId) { 64 Messenger callback = intent.getParcelableExtra("messenger"); 65 Message m = Message.obtain(); 66 m.what = MainActivity.MSG_SERVICE_OBJ; 67 m.obj = this; 68 try { 69 callback.send(m); 70 } catch (RemoteException e) { 71 Log.e(TAG, "Error passing service object back to activity."); 72 } 73 return START_NOT_STICKY; 74 } 75 76 @Override 77 public boolean onStartJob(JobParameters params) { 78 jobParamsMap.add(params); 79 if (mActivity != null) { 80 mActivity.onReceivedStartJob(params); 81 } 82 Log.i(TAG, "on start job: " + params.getJobId()); 83 return true; 84 } 85 86 @Override 87 public boolean onStopJob(JobParameters params) { 88 jobParamsMap.remove(params); 89 mActivity.onReceivedStopJob(); 90 Log.i(TAG, "on stop job: " + params.getJobId()); 91 return true; 92 } 93 94 MainActivity mActivity; 95 private final LinkedList<JobParameters> jobParamsMap = new LinkedList<JobParameters>(); 96 97 public void setUiCallback(MainActivity activity) { 98 mActivity = activity; 99 } 100 101 /** Send job to the JobScheduler. */ 102 public void scheduleJob(JobInfo t) { 103 Log.d(TAG, "Scheduling job"); 104 JobScheduler tm = 105 (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); 106 tm.schedule(t); 107 } 108 109 public boolean callJobFinished() { 110 JobParameters params = jobParamsMap.poll(); 111 if (params == null) { 112 return false; 113 } else { 114 jobFinished(params, false); 115 return true; 116 } 117 } 118 119} 120