We're implementing a Job via JobScheduler for background loading of data. The job will fire about once a day. What tools are available for us to test this functionality (possibly ADB)?
Use cases are to be able to simulate the conditions required for a Job to be run or to just say specifically "Run this job" as part of our automated test suite.
This is working for me, without the need to use adb commands. It requires minSdk 21
Right.
Henning and P4u144 set me on the right track to answer this in more detail.
Identify all registered jobs
Identify your task with the
adb shell dumpsys jobscheduler
command.This will give you a huge output in following categories.
The category you are most likely to be interested in is Registered XX Jobs. This tells you how many jobs have been scheduled on the device.
For example, your packagename is
com.foo.bar.application
you should see an entry like this:Now you can easily identify if your job has been registered with the correct criteria.
FireBaseJobdispatcher
If you use
FirebaseJobDispatcher
lib you can useto check if your service has been scheduled or run.
Force your task to run
When creating a
Job
you get aJOB_ID
returned.Use this
JOB_ID
to force the job to run.You can do this by using the
adb shell cmd jobscheduler run
command, (requires Android 7.1 or higher).For example, your packagename is
com.foo.bar.application
and theJOB_ID
was 1. You can now run your task viaadb
Don't forget the
-f
option as this forces the job to run even if the restrictions set are not met.Evernote Android Job Lib
Last but certainly not least.
Use the wonderful library from Evernote for this.
It allows for easy backporting of the
JobScheduler
on lower API levels using eitherJobScheduler
,GcmNetworkManager
orAlarmManager
depending on your API level.Edit 24/08
Even better use the firebase job dispatcher library.
I hope this helped.
Thanks
With the command
adb shell dumpsys jobscheduler
you get informations about the currently scheduled and active jobs.I noticed that the ouput from the command differs greatly between Android 6 and 7. With an Android 5 device the output is very short and sometimes cryptic. The interesting part with the registered jobs is build here and repeated below for convenience, which should help with the deciphering:
Android 7 devices on the other hand have a very long output with more detailed and better readable informations. Also there are more features like a history. The drawback is that you have to find the interesting parts first.
I haven't found a way to force a job to run though, there is a feature request for it.See the answer from p4u144.Starting in Android 7.0, there is a new
cmd
for adb shell. And in 7.1 (only in preview right now), theadb shell cmd jobscheduler
has been added as you can see here to force run your JobScheduler. The helps says: