Search code examples
androidandroid-workmanager

Is there a way to know if a WorkManager Job did not execute because of a constraint?


Right now I have some jobs on the WorkManager that uses this constraint.

wifiConstraint = Constraints.Builder()
                .setRequiredNetworkType(NetworkType.UNMETERED)
                .build()

work_version = "2.2.0"
implementation "androidx.work:work-runtime-ktx:$project.work_version"

The customers device seems to be connected to a WiFi network but the Job is not getting dispatched (it's set to repeatInterval: 30, TimeUnit.MINUTES) after 6 hours.

This WiFi network is an enterprise network with a configuration profile.

Is there a way to know if the Job did not meet an established constraint, and which constraint did not meet?

Thanks.


Solution

  • April 9th 2020 Update

    WorkManager v2.4.0 alpha 01 introduces the ability to request diagnostic information from WorkManager using

    adb shell am broadcast -a "androidx.work.diagnostics.REQUEST_DIAGNOSTICS" -p "<your_app_package_name>". 
    

    This provides a lot of useful information including:

    • WorkRequests that were completed in the last 24 hours.
    • WorkRequests that are currently RUNNING.
    • Scheduled WorkRequests. (aosp/1235501)

    This is only available on debug builds.

    Follow original answer, still valid for older WorkManager version and for release builds.

    You cannot get this informations directly in WorkManager, but, assuming that this device is running on Android 6.0+ (API level23+), you can get some information about the underlying JobScheduler API used by WorkManager.

    Using the command:

    adb shell dumpsys jobscheduler

    You should be able to identify your task with this command.

    This will give you a huge output in following categories.

    • Settings
    • Registered XX Jobs
    • Connectivity
    • Alarms
    • Idle
    • Battery
    • AppIdle
    • Content
    • Job history
    • Pending queue

    If you are testing on Android 7.1 and higher, then you can force-run a scheduled job using the following command.

    adb shell cmd jobscheduler run -f PACKAGE_NAME JOB_ID