如何避免JobScheduler中的IllegalStateException?

15

在Application.onCreate()方法中访问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());

你的意思是JobScheduler是在Application#onCreate()中创建的吗? - CoXier
技术上,该系统正在创建JobScheduler。 这就是我们在Application.onCreate()中所做的。myJobScheduler = (JobScheduler)getSystemService(Context.JOB_SCHEDULER_SERVICE); myJobScheduler.schedule(builder.build()); - Pavel
1
可能相关:https://dev59.com/TGMk5IYBdhLWcg3w6yDh。至少,那个问题解释了为什么上面的堆栈跟踪信息如此不具有信息性。 - Ilmari Karonen
1
考虑包含您的SourceFile...或者任何代码,以便查看更广泛的上下文,因为...builder.build()是什么?我的意思是这可以帮助任何人回答您的问题;) - Piotr Z
谢谢,Piotr。我已经添加了一个代码部分,用于调度工作。如果您需要更多细节,请告诉我。 - Pavel
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

框架作业调度程序支持API级别21及以上。 请查看下面的链接智能作业调度


作业调度器API支持API 21及更高版本。 你刚才说的是相反的,为什么? - Pavel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接