有哪些工具可以测试JobScheduler? [英] What tools are available to test JobScheduler?

查看:311
本文介绍了有哪些工具可以测试JobScheduler?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在通过 JobScheduler 实施工作数据加载.这项工作大约每天执行一次.哪些工具可用于我们测试此功能(可能是ADB)?

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.

推荐答案

正确.
Henning和P4u144使我走上正确的道路,以更详细地回答这个问题.

Right.
Henning and P4u144 set me on the right track to answer this in more detail.

使用adb shell dumpsys jobscheduler命令标识您的任务.
这将在以下类别中为您带来巨大的收益.

Identify your task with the adb shell dumpsys jobscheduler command.
This will give you a huge output in following categories.

  • 设置
  • 已注册的XX个职位
  • 连接性
  • 警报
  • 空闲
  • 电池
  • AppIdle
  • 内容
  • 工作经历
  • 待处理队列
  • Settings
  • Registered XX Jobs
  • Connectivity
  • Alarms
  • Idle
  • Battery
  • AppIdle
  • Content
  • Job history
  • Pending queue

您最可能感兴趣的类别是已注册XX职位.这将告诉您已在设备上计划了多少个作业.
例如,您的包名称为com.foo.bar.application,您应该看到如下所示的条目:

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:

JOB #u0a93/17: eec3709 com.foo.bar.application/com.evernote.android.job.v21.PlatformJobService
    u0a93 tag=*job*/com.foo.bar.application/com.evernote.android.job.v21.PlatformJobService
    Source: uid=u0a93 user=0 pkg=com.foo.bar.application
    JobInfo:
      Service: com.foo.bar.application/com.evernote.android.job.v21.PlatformJobService
      PERIODIC: interval=+15m0s0ms flex=+5m0s0ms
      PERSISTED
      Requires: charging=false deviceIdle=false
      Network type: 2
      Backoff: policy=1 initial=+30s0ms
      Has early constraint
      Has late constraint
    Required constraints: TIMING_DELAY DEADLINE UNMETERED
    Satisfied constraints: CONNECTIVITY NOT_ROAMING APP_NOT_IDLE DEVICE_NOT_DOZING
    Unsatisfied constraints: TIMING_DELAY DEADLINE UNMETERED
    Earliest run time: 07:23
    Latest run time: 12:23
    Ready: false (job=false pending=false active=false user=true)

提示:使用adb shell dumpsys jobscheduler | grep com.foo.bar.application快速过滤列表.

现在,您可以轻松地确定您的工作是否已经按照正确的条件进行了注册.

Now you can easily identify if your job has been registered with the correct criteria.

如果使用FirebaseJobDispatcher lib,则可以使用

If you use FirebaseJobDispatcher lib you can use

adb shell dumpsys activity service GcmService | grep com.foo.bar.debug
    com.foo.bar.debug:0 v853
    u0|com.foo.bar.debug: 3
    (scheduled) com.foo.bar.debug/com.firebase.jobdispatcher.GooglePlayReceiver{u=0 tag="com.foo.bar.debug.job.FetchArticlesJob" trigger=window{start=10800s,end=11700s,earliest=10448s,latest=11348s} requirements=[NET_UNMETERED,DEVICE_IDLE] attributes=[PERSISTED,RECURRING] scheduled=-351s last_run=N/A jid=N/A status=PENDING retries=0 client_lib=FIREBASE_JOB_DISPATCHER-1}
    (scheduled) com.foo.bar.debug/com.firebase.jobdispatcher.GooglePlayReceiver{u=0 tag="com.foo.bar.debug.job.FetchNotificationGroupsJob" trigger=window{start=86400s,end=129600s,earliest=86048s,latest=129248s} requirements=[NET_CONNECTED,CHARGING] attributes=[PERSISTED,RECURRING] scheduled=-351s last_run=N/A jid=N/A status=PENDING retries=0 client_lib=FIREBASE_JOB_DISPATCHER-1}
    (scheduled) com.foo.bar.debug/com.firebase.jobdispatcher.GooglePlayReceiver{u=0 tag="com.foo.bar.debug.job.RemoveUnusedRealmArticlesJob" trigger=window{start=577980s,end=608400s,earliest=521961s,latest=552381s} requirements=[NET_ANY] attributes=[PERSISTED,RECURRING] scheduled=-56018s last_run=N/A jid=N/A status=PENDING retries=0 client_lib=FIREBASE_JOB_DISPATCHER-1}
    (finished) [com.foo.bar.debug/com.firebase.jobdispatcher.GooglePlayReceiver:com.foo.bar.debug.job.UpdateNotificationGroupJob,u0]
    (finished) [com.foo.bar.debug/com.firebase.jobdispatcher.GooglePlayReceiver:com.foo.bar.debug.job.UpdatePushTokenJob,u0]
    (finished) [com.foo.bar.debug/com.firebase.jobdispatcher.GooglePlayReceiver:com.foo.bar.debug.job.FetchArticlesJob,u0]

检查您的服务是否已安排或运行.

to check if your service has been scheduled or run.

创建Job时,您将返回JOB_ID.
使用此JOB_ID强制作业运行.
您可以使用adb shell cmd jobscheduler run命令(需要Android 7.1或更高版本)来执行此操作.

When creating a Job you get a JOB_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).

例如,您的软件包名称是com.foo.bar.application,而JOB_ID是1. 现在,您可以通过adb

For example, your packagename is com.foo.bar.application and the JOB_ID was 1. You can now run your task via adb

adb shell cmd jobscheduler run -f com.foo.bar.application 1

别忘了-f选项,因为即使不满足设置的限制,这也会强制作业运行.

Don't forget the -f option as this forces the job to run even if the restrictions set are not met.

最后但并非最不重要.
为此,使用来自Evernote的精彩库.
可以根据您的API级别使用JobSchedulerGcmNetworkManagerAlarmManager在较低的API级别上轻松地反向移植JobScheduler.

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 either JobScheduler, GcmNetworkManager or AlarmManager depending on your API level.

更好地使用firebase作业分配程序库.

Even better use the firebase job dispatcher library.

Firebase JobDispatcher 是用于在Android应用中安排后台作业的库.它提供了与JobScheduler兼容的API,该API可以在安装了Google Play服务的所有最新版本的Android(API级别9+)上使用.

The Firebase JobDispatcher is a library for scheduling background jobs in your Android app. It provides a JobScheduler-compatible API that works on all recent versions of Android (API level 9+) that have Google Play services installed.

我希望这会有所帮助.

I hope this helped.

谢谢

Evernote Android-Job

Both Evernote Android-Job and Firebase JobDispatcher are now in maintenance only mode and they both suggest to use jetpack's WorkManager for these kind of jobs.

Android Jetpack WorkManager使其更易于诊断.
首先,您需要为您的包裹启用诊断功能

Android Jetpack WorkManager made it slightly easier to diagnose.
First you will need to enable diagnostics for your package

adb shell am broadcast -a 'androidx.work.diagnostics.REQUEST_DIAGNOSTICS' -p 'com.foo.bar.application'

之后,您可以将WorkManager转储到ADB Logcat中的信息,您可以通过输入信息进行观察.

After that you can WorkManager will dump information in ADB Logcat which you can observe by entering.

adb logcat

或通过Android Studio Logcat工具窗口

Or via Android Studio Logcat tool window

这篇关于有哪些工具可以测试JobScheduler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆