@@ -53,10 +53,13 @@ public class JobWorkService extends JobService {
53
53
/**
54
54
* This is a task to dequeue and process work in the background.
55
55
*/
56
- final class CommandProcessor extends AsyncTask <Void , Void , Void > {
56
+ static final class CommandProcessor extends AsyncTask <Void , Void , Void > {
57
57
private final JobParameters mParams ;
58
- CommandProcessor (JobParameters params ) {
59
- mParams = params ;
58
+ private final Logger logger ;
59
+ private final Context context ;
60
+
61
+ CommandProcessor (JobParameters params , Context context , Logger logger ) {
62
+ mParams = params ; this .logger = logger ; this .context = context ;
60
63
}
61
64
@ Override
62
65
protected Void doInBackground (Void ... params ) {
@@ -89,7 +92,7 @@ protected Void doInBackground(Void... params) {
89
92
callOnHandleIntent (intentService , work .getIntent ());
90
93
completeWork (mParams , work );
91
94
} else {
92
- Handler mainHandler = new Handler (getApplicationContext ().getMainLooper ());
95
+ Handler mainHandler = new Handler (context . getApplicationContext ().getMainLooper ());
93
96
final Service mainService = (Service )service ;
94
97
final JobWorkItem workItem = work ;
95
98
final Intent manServiceIntent = work .getIntent ();
@@ -121,6 +124,49 @@ public void run() {
121
124
}
122
125
return null ;
123
126
}
127
+ private void setContext (Service service ) {
128
+ callMethod (ContextWrapper .class , service , "attachBaseContext" , new Class [] { Context .class }, context .getApplicationContext ());
129
+ }
130
+
131
+ private void callOnStartCommand (Service service , Intent intent ) {
132
+ callMethod (Service .class , service , "onStartCommand" , new Class [] { Intent .class , int .class , int .class }, intent , 0 , 1 );
133
+ }
134
+
135
+ private void callOnHandleIntent (IntentService intentService , Intent intent ) {
136
+ callMethod (IntentService .class , intentService , "onHandleIntent" , new Class [] { Intent .class }, intent );
137
+ }
138
+
139
+ private void callMethod (Class clazz , Object object , String methodName , Class [] parameterTypes , Object ... parameters ) {
140
+ try {
141
+ Method method = clazz .getDeclaredMethod (methodName , parameterTypes );
142
+ method .setAccessible (true );
143
+ method .invoke (object , parameters );
144
+
145
+ } catch (NoSuchMethodException e ) {
146
+ logger .error ("Error calling method " + methodName , e );
147
+ } catch (InvocationTargetException e ) {
148
+ logger .error ("Error calling method " + methodName , e );
149
+ } catch (IllegalAccessException e ) {
150
+ logger .error ("Error calling method " + methodName , e );
151
+ }
152
+
153
+ }
154
+
155
+ private void completeWork (JobParameters jobParameters , JobWorkItem jobWorkItem ) {
156
+ Intent intent = jobWorkItem .getIntent ();
157
+ if (intent != null && intent .hasExtra (INTENT_EXTRA_JWS_PERIODIC )) {
158
+ logger .info ("Periodic work item completed " );
159
+ jobParameters .completeWork (jobWorkItem );
160
+ //reschedule(jobWorkItem);
161
+ }
162
+ else {
163
+ logger .info ("work item completed" );
164
+ jobParameters .completeWork (jobWorkItem );
165
+
166
+ }
167
+
168
+ }
169
+
124
170
}
125
171
@ Override
126
172
public void onCreate () {
@@ -134,7 +180,7 @@ public void onDestroy() {
134
180
@ Override
135
181
public boolean onStartJob (JobParameters params ) {
136
182
// Start task to pull work out of the queue and process it.
137
- mCurProcessor = new CommandProcessor (params );
183
+ mCurProcessor = new CommandProcessor (params , getApplicationContext (), this . logger );
138
184
mCurProcessor .executeOnExecutor (AsyncTask .THREAD_POOL_EXECUTOR );
139
185
// Allow the job to continue running while we process work.
140
186
return true ;
@@ -150,49 +196,6 @@ public boolean onStopJob(JobParameters params) {
150
196
return true ;
151
197
}
152
198
153
- private void setContext (Service service ) {
154
- callMethod (ContextWrapper .class , service , "attachBaseContext" , new Class [] { Context .class }, getApplicationContext ());
155
- }
156
-
157
- private void callOnStartCommand (Service service , Intent intent ) {
158
- callMethod (Service .class , service , "onStartCommand" , new Class [] { Intent .class , int .class , int .class }, intent , 0 , 1 );
159
- }
160
-
161
- private void callOnHandleIntent (IntentService intentService , Intent intent ) {
162
- callMethod (IntentService .class , intentService , "onHandleIntent" , new Class [] { Intent .class }, intent );
163
- }
164
-
165
- private void callMethod (Class clazz , Object object , String methodName , Class [] parameterTypes , Object ... parameters ) {
166
- try {
167
- Method method = clazz .getDeclaredMethod (methodName , parameterTypes );
168
- method .setAccessible (true );
169
- method .invoke (object , parameters );
170
-
171
- } catch (NoSuchMethodException e ) {
172
- logger .error ("Error calling method " + methodName , e );
173
- } catch (InvocationTargetException e ) {
174
- logger .error ("Error calling method " + methodName , e );
175
- } catch (IllegalAccessException e ) {
176
- logger .error ("Error calling method " + methodName , e );
177
- }
178
-
179
- }
180
-
181
- private void completeWork (JobParameters jobParameters , JobWorkItem jobWorkItem ) {
182
- Intent intent = jobWorkItem .getIntent ();
183
- if (intent != null && intent .hasExtra (INTENT_EXTRA_JWS_PERIODIC )) {
184
- logger .info ("Periodic work item completed " );
185
- jobParameters .completeWork (jobWorkItem );
186
- //reschedule(jobWorkItem);
187
- }
188
- else {
189
- logger .info ("work item completed" );
190
- jobParameters .completeWork (jobWorkItem );
191
-
192
- }
193
-
194
- }
195
-
196
199
private void reschedule (JobWorkItem item ) {
197
200
ServiceScheduler .PendingIntentFactory pendingIntentFactory = new ServiceScheduler
198
201
.PendingIntentFactory (getApplicationContext ());
0 commit comments