31
31
import android .support .annotation .RequiresApi ;
32
32
33
33
import org .slf4j .Logger ;
34
+ import org .slf4j .LoggerFactory ;
34
35
35
36
import static android .app .job .JobScheduler .RESULT_SUCCESS ;
36
37
@@ -131,10 +132,14 @@ private void setRepeating(long interval, PendingIntent pendingIntent, Intent int
131
132
132
133
builder .setExtras (persistableBundle );
133
134
134
- if (jobScheduler .schedule (builder .build ()) != RESULT_SUCCESS ) {
135
- logger .error ("ServiceScheduler" , "Some error while scheduling the job" );
135
+ try {
136
+ if (jobScheduler .schedule (builder .build ()) != RESULT_SUCCESS ) {
137
+ logger .error ("ServiceScheduler" , "Some error while scheduling the job" );
138
+ }
139
+ }
140
+ catch (Exception e ) {
141
+ logger .error (String .format ("Problem scheduling job %s" , intent .getComponent ().toShortString ()), e );
136
142
}
137
-
138
143
}
139
144
else {
140
145
AlarmManager alarmManager = (AlarmManager ) context .getSystemService (Context .ALARM_SERVICE );
@@ -155,11 +160,7 @@ private void cancelRepeating(PendingIntent pendingIntent, Intent intent) {
155
160
if (ServiceScheduler .isScheduled (context , id )) {
156
161
jobScheduler .cancel (id );
157
162
}
158
- } catch (IllegalAccessException e ) {
159
- logger .error ("Error in Cancel " , e );
160
- } catch (NoSuchFieldException e ) {
161
- logger .error ("Error in Cancel " , e );
162
- } catch (ClassNotFoundException e ) {
163
+ } catch (Exception e ) {
163
164
logger .error ("Error in Cancel " , e );
164
165
}
165
166
}
@@ -171,16 +172,14 @@ private void cancelRepeating(PendingIntent pendingIntent, Intent intent) {
171
172
}
172
173
173
174
private int getJobId (Intent intent ) {
174
- String clazz = intent . getComponent (). getClassName () ;
175
+ String clazz = "unknown" ;
175
176
Integer id = null ;
177
+
176
178
try {
179
+ clazz = intent .getComponent ().getClassName ();
177
180
id = (Integer ) Class .forName (clazz ).getDeclaredField ("JOB_ID" ).get (null );
178
- } catch (IllegalAccessException e ) {
181
+ } catch (Exception e ) {
179
182
logger .error ("Error getting JOB_ID from " + clazz , e );
180
- } catch (NoSuchFieldException e ) {
181
- logger .error ("Error getting JOB_ID from " + clazz , e );
182
- } catch (ClassNotFoundException e ) {
183
- logger .error ("Error getting JOB_ID from " + clazz , e );
184
183
}
185
184
186
185
return id == null ? -1 : id ;
@@ -197,7 +196,7 @@ public void unschedule(Intent intent) {
197
196
try {
198
197
PendingIntent pendingIntent = pendingIntentFactory .getPendingIntent (intent );
199
198
cancelRepeating (pendingIntent , intent );
200
- logger .info ("Unscheduled {}" , intent .getComponent (). toShortString ());
199
+ logger .info ("Unscheduled {}" , intent .getComponent ()!= null ? intent . getComponent (). toShortString () : "intent" );
201
200
} catch (Exception e ) {
202
201
logger .debug ("Failed to unschedule service" , e );
203
202
}
@@ -274,7 +273,13 @@ public static void startService(Context context, Integer jobId, Intent intent) {
274
273
.build ();
275
274
JobScheduler jobScheduler = (JobScheduler ) context .getSystemService (Context .JOB_SCHEDULER_SERVICE );
276
275
277
- jobScheduler .enqueue (jobInfo , new JobWorkItem (intent ));
276
+ JobWorkItem jobWorkItem = new JobWorkItem (intent );
277
+ try {
278
+ jobScheduler .enqueue (jobInfo , jobWorkItem );
279
+ }
280
+ catch (Exception e ) {
281
+ LoggerFactory .getLogger ("ServiceScheduler" ).error ("Problem enqueuing work item " , e );
282
+ }
278
283
279
284
}
280
285
else {
0 commit comments