Firebase JobDispatcher-与以前的API(JobScheduler和GcmTaskService)相比,它如何工作? [英] Firebase JobDispatcher - how does it work compared to previous APIs (JobScheduler and GcmTaskService)?

查看:130
本文介绍了Firebase JobDispatcher-与以前的API(JobScheduler和GcmTaskService)相比,它如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google为作业/任务计划提供了多种解决方案,例如JobSchedulerGcmTaskService.每种都有自己的优点和缺点.

Google has multiple solutions for job/task scheduling, such as JobScheduler and GcmTaskService. Each has its own advantages and disadvantages.

最近,Google推出了一个名为"Firebase JobDispatcher"的新库.

Recently, Google presented a new library called "Firebase JobDispatcher".

可悲的是,关于这个新API的知识很少.实际上,真的很难找到任何东西.

Sadly, there is very little to read about this new API. In fact, it's really hard to find anything about it.

我唯一发现的是他们的公告视频

Only thing I've found is their announcement video and a sample. But even their, there is not much to know about this API.

看看以前与其他API的问题,调查和比较(例如,此处),我是d想询问新API的工作原理,并知道在使用新API时要考虑的因素:

Looking at previous questions, investigations and comparisons I had with the other APIs (here, for example), I'd like to ask how the new API works and know what to take into consideration when using it:

  1. 作业是否可以保留参数,甚至可以在需要时进行修改?他们在示例中说:"可选的用户提供的捆绑包.默认捆绑包为空."是这样吗?可以在作业执行时对其进行修改吗?

  1. Can a job have parameters that stay with it and can even be modified when needed? They say in the sample "An optional Bundle of user-supplied extras. The default is an empty Bundle." Is this it? Can it be modified by the job upon execution of it?

可以轻松重新安排作业吗?有人说"指示作业是否应该重复的布尔值".如何选择何时重新安排?我已经尝试过该示例,并选择了"Recurring",但它似乎没有再次运行,仅运行了一次.

Can jobs be re-scheduled easily? It is said "A boolean indicating whether the Job should repeat" . How can it be chosen when to re-schedule? I've tried the sample, and chose "Recurring", but it doesn't seem to run again, only once.

相对于图书馆的工作,可以保护它吗(由于具有唯一的ID)?

Can it be protected vs library's jobs (because of unique ids)?

在更新应用程序时是否需要格外小心(就像以前的API一样)?更新应用程序后仍可以安排工作吗?在样本上进行测试,似乎工作在应用程序更新后完全消失了.可以避免吗?

Does it needs extra care when updating the app (as previous APIs did)? Can jobs still be scheduled after an update of the app? Testing on the sample, it seems the jobs are completely gone after an update of the app. Can it be avoided?

是否需要RECEIVE_BOOT_COMPLETED,以防即使重新启动设备后仍要计划作业?该示例似乎有它.

Does it need RECEIVE_BOOT_COMPLETED in case I want the job to still be scheduled even when the device is restarted? The sample seems to have it.

是否可以获取所有已调度作业及其信息(包括参数)的列表,并能够取消特定/全部作业甚至进行修改?

Is it possible to get a list of all scheduled jobs and their information (including parameters), and be able to cancel specific/all of them and even modify them ?

在应用程序执行清除数据操作后会删除作业吗?

Will a job be removed upon clear-data operation of the app?

是否可以告诉工作它最好在一定时间范围内运行(例如:早晨7:00至8:00之间)?提到了" ExecutionWindowTrigger-指定应在其中执行作业的时间窗口".是吗错过此窗口会发生什么?

Is it possible to tell the job that it's best that it will run in a range of time (example : between 7:00 and 8:00 in the morning)? It is mentioned "ExecutionWindowTrigger-which specifies a time window in which the Job should be executed". Is that it? What happens when it misses this window?

JobService类中的方法onStartJob返回一个布尔值,其描述为"是否还有更多工作要做."是什么意思? jobFinished方法的needsReschedule参数是什么意思?它们彼此相关吗?

The method onStartJob in JobService class return a boolean and the description for it is "whether there is more work remaining." What does it mean? What does the needsReschedule parameter of jobFinished method mean? Are they related to each other?

我应该知道哪些限制?例如最小&每个功能的最大值?

Are there any restrictions I should know about? For example minimal & maximal values for each of the functions?

推荐答案

实际上Firebase Android JobDispatcher是围绕Android上的作业调度引擎的抽象层. 目前,他们只有一种用于GCM Network Manager的驱动程序实施. 这意味着当前它的行为与 GCM网络管理器的行为相同.希望将来会实施更多的驱动程序.

Actually Firebase Android JobDispatcher is a layer of abstraction around job scheduling engines on Android. And for now they only have one driver implementation for GCM Network Manager. That means currently it behaves the same way as GCM Network Manager behaves. Hopefully in the future more Drivers will be implemented.

1.作业是否可以保留参数,甚至可以在需要时进行修改?他们在示例中说:可选的用户提供的附加捆绑包.默认值为空捆绑包." .是这个吗?可以在作业执行时对其进行修改吗?

  1. 是的,Job.Builder具有带有任意束的方法setExtras,以后可以通过jobParameters.getExtras()进行访问. 您不能修改包(jobParameters仅包含吸气剂).您可以使用标志setReplaceCurrent(true)重新安排您的工作,并指定一个新的捆绑包.
  1. Yes, Job.Builder has method setExtras with arbitrary bundle which later may be accessed via jobParameters.getExtras(). You cannot modify the bundle (jobParameters contains only getters). You could reschedule your job with flag setReplaceCurrent(true) and specify a new bundle.

2.可以轻松地重新安排工作吗?有人说指示作业是否应该重复的布尔值".如何选择何时重新安排?我已经尝试过该示例,然后选择重复发生",但它似乎没有再次运行,只能运行一次.

  1. 要重新计划作业,您需要指定setRecurring(true)setTrigger(Trigger.executionWindow(10, 20))

窗口开始截止时间一到,就会立即触发 到达,并鼓励驱动程序在窗口之前运行作业 如果可能的话结束.

This becomes triggered as soon as the window start deadline is reached, and drivers are encouraged to run the Job before the window end if possible.

3.相对于图书馆的工作,可以保护它吗(由于具有唯一的ID)?

  1. 作业标签在您的应用程序中必须是唯一的.手机上的其他应用程序都有自己的端点"(程序包名称/服务名称). 要查看GooglePlayDriver的所有计划/完成的作业,请使用

  1. Job tags must be unique in your application. Other apps on the phone have their own 'endpoints' (package name/service name). To see all scheduled/finished jobs for GooglePlayDriver please use

adb shell dumpsys activity service GcmService

4.在更新应用程序时是否需要格外小心(就像以前的API一样)?更新应用程序后还能安排工作吗?在样本上进行测试,似乎工作在应用程序更新后完全消失了.可以避免吗?

  1. 对于GCM Network Manager,GooglePlayDriver不会在Google Play服务或应用程序更新后重新计划作业. 这是未解决的问题.因此,现在这是您的责任.
  1. As for GCM Network Manager GooglePlayDriver doesn't reschedule Jobs after Google Play Services or the app is updated. Here is an open issue for this. So for now this is your responsibility.

5.如果我希望即使重新启动设备后仍要计划作业,是否需要RECEIVE_BOOT_COMPLETED?该示例似乎有它.

  1. 是的,您需要这种许可. Builder具有控制行为的参数:setLifetime(Lifetime.FOREVER | UNTIL_NEXT_BOOT) 当然,如果您要创建自己的驱动程序,则必须自己照顾一生.
  1. Yes you need such permission. Builder has a parameter to control the behavior: setLifetime(Lifetime.FOREVER | UNTIL_NEXT_BOOT) Of course if you're going to create your own driver you'll have to take care of the lifetime yourself.

6.是否可以获取所有已调度作业及其信息(包括参数)的列表,并能够取消特定/全部作业甚至进行修改?

  1. 否,与GCM网络管理器相同. 但是您可以自己安排所有工作以安排服务,从而以某种方式跟踪所有工作.
  1. No, the same as for GCM Network Manager. But you could track all jobs yourself somehow while scheduling them to play services.

7.应用清除数据操作后会删除作业吗?

  1. 是的,该作业将被删除.可能在先前版本的Google Play服务中,其行为有所不同.

8.是否可以告诉工作最好在一定时间范围内运行(例如:早晨7:00至8:00之间)?提到了"ExecutionWindowTrigger,它指定应在其中执行作业的时间窗口".是吗错过此窗口会发生什么?

  1. 好吧,您可以设置要在7:00触发的警报,并使用executionWindow(0, 60*60)安排一项非经常性作业.作业将在7:00-8:00之间运行.
    您无法使用周期性工作,因为
  1. Well, you could setup an alarm to be fired at 7:00 and schedule a non-recurring job with executionWindow(0, 60*60). The job will run between 7:00 - 8:00.
    You cannot use recurring job because

windowStart-作业应最早的时间(以秒为单位) 被认为有资格参加比赛.从安排作业的时间开始计算 (对于新工作)或上次运行(对于周期性工作).

windowStart - The earliest time (in seconds) the job should be considered eligible to run. Calculated from when the job was scheduled (for new jobs) or last run (for recurring jobs).

此外,ExecutionWindowTrigger指定近似时间.不保证它将在给定的窗口中运行. 如果错过了窗口,该作业将在以后的任何时间运行.

Also, ExecutionWindowTrigger specifies approximate time. It's not guaranteed it would run at the given window. If it misses the window the job will run any time later.

9. "JobService"类中的方法"onStartJob"返回一个布尔值,其描述为是否还有更多工作要做". .这是什么意思? "jobFinished"方法的"needsReschedule"参数是什么意思?它们彼此相关吗?

  1. 如果onStartJob返回false,则表示您已完成工作.无需调用jobFinished. RESULT_SUCCESS是自动发送的.
  1. if onStartJob returns false that means you completed your work. No Need to invoke jobFinished. The RESULT_SUCCESS is sent automatically.

如果onStartJob返回true,则表示您启动了一个线程并等待结果.完成后,您必须调用jobFinished来通知Google Play服务是否应重新安排作业.如果是,将根据RetryStrategy重新安排作业.

if onStartJob returns true that means you started a thread and waiting for results. As soos as you're done you must invoke jobFinished to inform google play services whether the job should be rescheduled or not. If yes the job will be rescheduled depending on RetryStrategy.

10.我应该知道什么限制吗?例如每个功能的最小值和最大值?

    • onStartJob应该尽快将工作卸载到另一个执行线程.他们提供SimpleJobService作为您期望的示例.
    • Lollipop的JobScheduler没有驱动程序实现.当Google Play服务不可用时,还需要处理这种情况,我们可能应该基于AlarmManager实现Driver.
    • onStartJob should offload work to another thread of execution as soon as possible. They provide a SimpleJobService as an example of what is expected from you.
    • There is no Driver implementation for Lollipop's JobScheduler. Also need to handle the situation when google play services are not available, we should probably implement Driver based on AlarmManager.

这篇关于Firebase JobDispatcher-与以前的API(JobScheduler和GcmTaskService)相比,它如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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