首页 > 解决方案 > 应用程序不能使用 JobIntentService 安排超过 100 个不同的作业

问题描述

这是我的JobIntentService

public class NotifierService extends JobIntentService {
     public static void enqueueWork(Context context, Intent work) {
         enqueueWork(context, NotifierService.class, JOB_ID, work);
     }

     protected void onHandleWork(Intent intent) {
         //simple if/else check
     }
}

但是,我从抱怨的 crashlytics 中遇到了很多崩溃100 distinct jobs

Caused by java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs
   at android.os.Parcel.readException(Parcel.java:1951)
   at android.os.Parcel.readException(Parcel.java:1889)
   at android.app.job.IJobScheduler$Stub$Proxy.enqueue(IJobScheduler.java:211)

有什么理由解决吗?

在将新的排队之前,我曾尝试取消它,如下所示:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
            if (scheduler != null) {
                for (JobInfo jobInfo : scheduler.getAllPendingJobs()) {
                    if (jobInfo.getId() == JOB_ID) {
                        scheduler.cancel(JOB_ID);
                    }
                }
            }
        }

但是,我遇到了另一个问题:

Caused by java.lang.IllegalArgumentException: Given work is not active: JobWorkItem{id=1 intent=Intent { cmp=com.my.test.package/.notifier.NotifierService } dcount=1}
   at android.app.job.JobParameters.completeWork(JobParameters.java:221)

你们有什么建议吗?

P/s:我无法在我的设备上重现它,但我在 CrashReport 工具中得到了数千次。在加入新工作之前取消工作确实解决了 100 个不同的工作问题,但它引发了如上所述的另一个错误。

标签: androidjobintentservice

解决方案


推荐阅读