我在我的应用程序中使用 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 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()
如果引发异常(例如没有连接)。