android - 为什么我看到 : Unsatisfied constraints: CONNECTIVITY in JobScheduler debug info

标签 android android-workmanager android-jobscheduler

我为这份工作准备:

 val job = JobInfo.Builder(2, ComponentName(context, JobRunner::class.java)).apply {
            setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
            setRequiresBatteryNotLow(true)
            for (triggerUri in triggers) {
                addTriggerContentUri(JobInfo.TriggerContentUri(triggerUri, JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS))
            }
            setTriggerContentUpdateDelay(TimeUnit.MINUTES.toMillis(1))
            setTriggerContentMaxDelay(TimeUnit.MINUTES.toMillis(10))
        }.build()
        
        jobScheduler.schedule(job)

如果我执行:adb shell dumpsys jobscheduler

我有以下工作信息:

JOB #u0a302/2: 89f4090 com.abc.debug/com.abc.mobile.service.job.JobRunner
    u0a302 tag=*job*/com.abc.debug/com.abc.mobile.service.job.JobRunner
    Source: uid=u0a302 user=0 pkg=com.abc.debug
    JobInfo:
      Service: com.abc.debug/com.abc.mobile.service.job.JobRunner
      Internal flags: 1 HAS_FOREGROUND_EXEMPTION
      Requires: charging=false batteryNotLow=true deviceIdle=false
      Trigger content URIs:
        1 content://media/internal/images/media
        1 content://media/external/images/media
        1 content://media/internal/video/media
        1 content://media/external/video/media
      Trigger update delay: +1m0s0ms
      Trigger max delay: +10m0s0ms
      Network type: NetworkRequest [ NONE id=0, [ Capabilities: NOT_METERED&INTERNET&NOT_RESTRICTED&TRUSTED&VALIDATED Unwanted:  Uid: 10302] ]
      Backoff: policy=1 initial=+30s0ms
    Required constraints: BATTERY_NOT_LOW CONNECTIVITY CONTENT_TRIGGER [0x14000002]
    Satisfied constraints: CHARGING BATTERY_NOT_LOW CONTENT_TRIGGER DEVICE_NOT_DOZING BACKGROUND_NOT_RESTRICTED [0x6400003]
    Unsatisfied constraints: CONNECTIVITY [0x10000000]
    Tracking: BATTERY CONNECTIVITY CONTENT
    Standby bucket: RARE
    Base heartbeat: 285
    Enqueue time: -20h50m38s25ms
    Run time: earliest=none, latest=none
    Last run heartbeat: 285
    Ready: false (job=false user=true !pending=true !active=true !backingup=true comp=true)

从上面的日志可以看出我需要:

网络类型:NetworkRequest [ NONE id=0, [ Capabilities: NOT_METERED&INTERNET&NOT_RESTRICTED&TRUSTED&VALIDATED Unwanted: Uid: 10302] ]

我在同一台设备上制作了另一个小应用程序来检查 Activity 网络功能:

 val connMgr = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
 val networkCap = connMgr.getNetworkCapabilities(connMgr.activeNetwork)
 Log.d("NETWORK_CHECK", "NetworkCapabilities: [$networkCap]")

结果可见:

[ 传输:WIFI 功能:NOT_METERED&INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN&VALIDATED&NOT_ROAMING&FOREGROUND&NOT_CONGESTED&NOT_SUSPENDED LinkUpBandwidth>=1048576Kbps LinkDnBandwidth>=1048576Kbps 信号强度:-43]

我拥有我需要的所有能力。为什么我仍然看到约束不满足?

未满足的约束:CONNECTIVITY [0x10000000]

是否有其他可能相关但不完全是能力的东西?

我已经检查过这里,但没有发现任何有用的东西:ConnectivityController

我从另一个日志中发现连接部分的以下内容:

ConnectivityController:
  Requested standby exceptions: 10336 (1 jobs) 10342 (1 jobs) 10391 (1 jobs)

10391 是我的 ID。 Requested standby exceptions 是什么意思?

因此,根据以下建议,我很可能是网络使用受限,因为我处于 RARE 备用状态。请在此处查看:

https://developer.android.com/topic/performance/power/power-details

我的问题是,在上面的工作信息中:从上次触发开始已经过去了 ~21 小时,并且通常在某些手机上我什至可以看到 2 天。但是文档说:

最多延迟 24 小时

所以我想找到更多关于到底发生了什么的信息,以便我可以改进。我们发现的不多。

但是禁用电池优化会产生巨大的差异。此外,当设备正在充电时,我们也没有问题。

https://developer.android.com/training/monitoring-device-state/doze-standby#support_for_other_use_cases

最佳答案

请务必注意,您的应用程序位于RARE standy bucket中:

 Standby bucket: RARE

这意味着,即使设备具有连接性,您的作业每天也只能访问一次网络。

这记录在 Power Manager restriction 中带注释的指南:

If network access is restricted, the app is granted a window of 10 minutes to use the network at the specified interval.

这可能是一个在后台花费大量时间而用户很少或根本没有交互的应用程序。一种可能的解决方案是增加前台时间,添加一些促使用户打开应用的功能。

关于android - 为什么我看到 : Unsatisfied constraints: CONNECTIVITY in JobScheduler debug info,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64878968/

相关文章:

android - 空指针异常 : FragmentManager. beginTransaction()

android - 当我按下后退按钮时,我的应用程序立即退出,不等待用户确认

android - Workmanager 无法在 android 12 Android kotlin 中延迟工作

android - 使用 android WorkManager 响应 Native HeadlessJs 任务调用

android - 从 JobService 观察 LiveData

android - 当 setIntent 后跟旋转时,getIntent 返回错误的 Intent

Android Lollipop 网络 API 和 OkHttp

android - 使用 WorkManager 和 Notification Service 在服务器上上传媒体

android - 通过 WorkManager API 在每个星期五安排一个特定任务

用于在后台运行日常任务的 Android WorkManager api