| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.demo.jobSchedulerApp.service; |
| 18 |
|
| 19 |
import android.app.job.JobInfo; |
| 20 |
import android.app.job.JobScheduler; |
| 21 |
import android.app.job.JobParameters; |
| 22 |
import android.app.job.JobService; |
| 23 |
import android.content.ComponentName; |
| 24 |
import android.content.Context; |
| 25 |
import android.content.Intent; |
| 26 |
import android.os.AsyncTask; |
| 27 |
import android.os.Message; |
| 28 |
import android.os.Messenger; |
| 29 |
import android.os.RemoteException; |
| 30 |
import android.util.Log; |
| 31 |
import android.util.SparseArray; |
| 32 |
import android.widget.Toast; |
| 33 |
|
| 34 |
import com.android.demo.jobSchedulerApp.MainActivity; |
| 35 |
|
| 36 |
import java.util.HashMap; |
| 37 |
import java.util.LinkedList; |
| 38 |
import java.util.Random; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public class TestJobService extends JobService { |
| 54 |
private static final String TAG = "SyncService"; |
| 55 |
|
| 56 |
@Override |
| 57 |
public void onCreate() { |
| 58 |
super.onCreate(); |
| 59 |
Log.i(TAG, "Service created"); |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public void onDestroy() { |
| 64 |
super.onDestroy(); |
| 65 |
Log.i(TAG, "Service destroyed"); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public int onStartCommand(Intent intent, int flags, int startId) { |
| 70 |
Messenger callback = intent.getParcelableExtra("messenger"); |
| 71 |
Message m = Message.obtain(); |
| 72 |
m.what = MainActivity.MSG_SERVICE_OBJ; |
| 73 |
m.obj = this; |
| 74 |
try { |
| 75 |
callback.send(m); |
| 76 |
} catch (RemoteException e) { |
| 77 |
Log.e(TAG, "Error passing service object back to activity."); |
| 78 |
} |
| 79 |
return START_NOT_STICKY; |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public boolean onStartJob(JobParameters params) { |
| 84 |
Log.i(TAG, "on start job: " + params.getJobId() |
| 85 |
+ " deadline?=" + params.isOverrideDeadlineExpired()); |
| 86 |
currentId++; |
| 87 |
jobParamsMap.put(currentId, params); |
| 88 |
final int currId = this.currentId; |
| 89 |
if (mActivity != null) { |
| 90 |
mActivity.onReceivedStartJob(params); |
| 91 |
} |
| 92 |
|
| 93 |
Toast.makeText( |
| 94 |
this, "On start job: '" + params.getJobId() + "' deadline exceeded: " + |
| 95 |
params.isOverrideDeadlineExpired(), |
| 96 |
Toast.LENGTH_LONG).show(); |
| 97 |
|
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
@Override |
| 103 |
public boolean onStopJob(JobParameters params) { |
| 104 |
Log.i(TAG, "on stop job: " + params.getJobId()); |
| 105 |
int ind = jobParamsMap.indexOfValue(params); |
| 106 |
jobParamsMap.remove(ind); |
| 107 |
mActivity.onReceivedStopJob(); |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
static int currentId = 0; |
| 112 |
MainActivity mActivity; |
| 113 |
private final SparseArray<JobParameters> jobParamsMap = new SparseArray<JobParameters>(); |
| 114 |
|
| 115 |
|
| 116 |
public void setUiCallback(MainActivity activity) { |
| 117 |
mActivity = activity; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
public void scheduleJob(JobInfo job) { |
| 122 |
Log.d(TAG, "Scheduling job " + job); |
| 123 |
JobScheduler tm = |
| 124 |
(JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); |
| 125 |
tm.schedule(job); |
| 126 |
} |
| 127 |
|
| 128 |
public boolean callJobFinished() { |
| 129 |
if (jobParamsMap.size() == 0) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
JobParameters params = jobParamsMap.valueAt(0); |
| 133 |
if (params == null) { |
| 134 |
return false; |
| 135 |
} else { |
| 136 |
jobFinished(params, false); |
| 137 |
jobParamsMap.removeAt(0); |
| 138 |
return true; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
} |