如何避免JobScheduler中的IllegalStateException?

问题描述 投票:13回答:2

在Application.onCreate()方法中访问JobScheduler时,我们在JobScheduler实现中观察到罕见的IllegalStateException。我想知道这是否是一个平台缺陷?

我们正在观察用户设备上的崩溃。几乎所有这些都是Android 5和5.1,但在Android 6(三星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)

源代码

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());
android android-jobscheduler
2个回答
1
投票

您可能正在遇到此处描述的异常: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)

在这种情况下,您希望避免安排超过100个作业。


-2
投票

Framework Job Scheduler支持以上API级别21.请浏览intelligent job scheduling下面的链接

© www.soinside.com 2019 - 2024. All rights reserved.