我已经在我的应用程序中使用WorkManager API实现了独特的定期工作。工作必须每30分钟检查一次在线资源,并在有未读通知的情况下显示通知。是的,我需要定期工作,因为资源是IMAP服务器,因此我无法使用FCM通知。但是,正如我在dumpsys jobscheduler
中看到的那样,已正确安排了作业,但是稍后执行了作业。当我运行dumpsys jobscheduler
时,我会看到类似的内容:
JOB #u0a360/7: aa1b828 com.mypackage.app/androidx.work.impl.background.systemjob.SystemJobService
u0a360 tag=*job*/com.mypackage.app/androidx.work.impl.background.systemjob.SystemJobService
Source: uid=u0a360 user=0 pkg=com.mypackage.app
JobInfo:
Service: com.mypackage.app/androidx.work.impl.background.systemjob.SystemJobService
Requires: charging=false batteryNotLow=false deviceIdle=false
Extras: mParcelledData.dataSize=180
Minimum latency: +29m59s973ms
Backoff: policy=1 initial=+30s0ms
Has early constraint
Required constraints: TIMING_DELAY [0x80000000]
Satisfied constraints: TIMING_DELAY DEVICE_NOT_DOZING BACKGROUND_NOT_RESTRICTED [0x82400000]
Unsatisfied constraints: WITHIN_QUOTA [0x1000000]
Tracking: TIME QUOTA
Implicit constraints:
readyNotDozing: true
readyNotRestrictedInBg: true
Standby bucket: FREQUENT
Base heartbeat: 0
Deferred since: -1h47m30s949ms
Enqueue time: -1h48m0s986ms
Run time: earliest=-1h18m1s13ms, latest=none, original latest=none
Last run heartbeat: 0
Ready: false (job=false user=true !pending=true !active=true !backingup=true comp=true)
这是问题Unsatisfied constraints: WITHIN_QUOTA [0x1000000]
,但是很遗憾,我无法找到有关此类错误的文档。谷歌官方发布的内容非常模糊:https://developer.android.com/topic/libraries/architecture/workmanager/how-to/debugging
这是我安排工作的方式:
if (PreferenceController.getInstance().getEmailNotificationInerval() != -1) {
int interval = PreferenceController.getInstance().getEmailNotificationInerval();
Constraints constraints = new Constraints.Builder().build();
PeriodicWorkRequest emailsRequest = new PeriodicWorkRequest.Builder(CheckNewEmailWorker.class, interval, TimeUnit.SECONDS)
.setConstraints(constraints)
.build();
WorkManager.getInstance(context).enqueueUniquePeriodicWork(CheckNewEmailWorker.TAG, ExistingPeriodicWorkPolicy.KEEP, emailsRequest);
}
并且我使用BOOT_COMPLETED
接收器在启动时启动它。
在工作程序中,如果一切正常,我返回Result.success()
,如果出现异常(例如,无连接),则返回Result.failure()
。
您的约束不满意。这就是为什么您的Worker
未被执行的原因。
Unsatisfied constraints: WITHIN_QUOTA [0x1000000]
这通常意味着您的应用程序正在运行太多的作业计划程序作业。这就是为什么您用尽quota
。另外,您的quota
由您可能要进入的app standby存储桶决定。
我们添加了一些文档来帮助诊断:https://developer.android.com/topic/libraries/architecture/workmanager/how-to/debugging