Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid IllegalStateException in JobScheduler?

We observe a rare IllegalStateException inside JobScheduler implementation when JobScheduler is accessed within Application.onCreate() method. I wonder if this is a platform flaw?

We are observing this crash on our users devices. Almost all of them are Android 5 and 5.1, but one crash happened on Android 6 (Samsung Galaxy S5 Duos).

java.lang.IllegalStateException: 
  at android.os.Parcel.readException (Parcel.java:1711)
  at android.os.Parcel.readException (Parcel.java:1653)
  at android.app.job.IJobScheduler$Stub$Proxy.schedule (IJobScheduler.java:158)
  at android.app.JobSchedulerImpl.schedule (JobSchedulerImpl.java:42)
  at yo.host.job.a.a (SourceFile:237)
  at yo.widget.WidgetController.b (SourceFile:92)
  at yo.host.Host.q (SourceFile:680)
  at yo.host.Host.onCreate (SourceFile:505)
  at android.app.Instrumentation.callApplicationOnCreate (Instrumentation.java:1032)
  at android.app.ActivityThread.handleBindApplication (ActivityThread.java:5970)

Source code

int jobId = 1;
JobInfo.Builder builder = new JobInfo.Builder(
    jobId,
    new ComponentName(
        Host.geti().getPackageName(),
        WeatherJobService.class.getName()
    )
);

builder.setPersisted(true);//Restart the job after reboot.
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);

PersistableBundle bundle = new PersistableBundle();
bundle.putString(WeatherJobService.EXTRA_LOCATION_ID, locationId);
bundle.putString(WeatherJobService.EXTRA_REQUEST_ID, requestId);
bundle.putString(WeatherJobService.EXTRA_CLIENT_ITEM, clientItem);
builder.setExtras(bundle);

int errorCode = getJobScheduler().schedule(builder.build());
like image 401
Pavel Avatar asked Feb 28 '18 09:02

Pavel


1 Answers

You might be experiencing the exception described here: https://github.com/yigit/android-priority-jobqueue/issues/202

Fatal Exception: java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs
  at android.os.Parcel.readException(Parcel.java:1674)
  at android.os.Parcel.readException(Parcel.java:1619)
  at android.app.job.IJobScheduler$Stub$Proxy.schedule(IJobScheduler.java:158)
  at android.app.JobSchedulerImpl.schedule(JobSchedulerImpl.java:42)

In which case you'd want to avoid scheduling more than 100 jobs.

like image 106
Greyson Parrelli Avatar answered Oct 10 '22 18:10

Greyson Parrelli