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