web 定制工作日历_定制工作经理的基础

web 定制工作日历

Welcome to the fifth post in our WorkManager series. WorkManager is an Android Jetpack library that runs deferrable, guaranteed background work when the work’s constraints are satisfied. It is the current best practice for this kind of work on Android.

欢迎来到我们的WorkManager系列中的第五篇文章。 WorkManager是一个Android Jetpack库,可在满足工作约束时运行可延迟的有保证的后台工作。 这是在Android上进行此类工作的当前最佳实践。

If you’ve been following thus far, we’ve talked about:

如果您到目前为止一直在关注,我们已经讨论过:

In this article, we’re going to talk about custom configuration, covering:

在本文中,我们将讨论自定义配置,内容包括:

  • Why we may need a custom configuration

    为什么我们可能需要自定义配置
  • How to define a custom configuration

    如何定义自定义配置
  • What is a WorkerFactory and why we need a custom one

    什么是WorkerFactory ,为什么我们需要自定义对象

  • What is the DelegatingWorkerFactory

    什么是DelegatingWorkerFactory

There’s a sixth blog post that expands these concepts to Dependency Injection and Dagger in particular that covers:

第六篇博客文章将这些概念扩展到了依赖注入和Dagger,特别涉及:

  • Use Dagger to inject parameters in our WorkerFactory

    使用Dagger在我们的WorkerFactory注入参数

  • On-Demand initialization

    按需初始化

The two articles are connected and the second one requires knowledge presented in this one.

这两篇文章是相互联系的,第二篇文章则需要本篇文章中介绍的知识。

说明问题 (Stating the problem)

When using WorkManager, it’s your responsibility to define Worker/CoroutineWorker or any other ListenableWorker derived classes. WorkManager instantiates your workers, at the right time, independently from having your application running in the foreground or running at all. To instantiate your worker class WorkManager uses a WorkerFactory.

使用WorkManager时,您有责任定义Worker / CoroutineWorker或任何其他ListenableWorker派生类。 WorkManager在适当的时间实例化您的工作人员,而与让应用程序在前台运行或完全运行无关。 要实例化您的工作者类,WorkManager使用WorkerFactory

Workers created by the default WorkerFactory have access to only 2 parameters:

由默认WorkerFactory创建的WorkerFactory只能访问2个参数:

If you need to have additional parameters passed to your worker’s constructor, you need a custom WorkerFactory.

如果需要将其他参数传递给工作人员的构造函数,则需要一个自定义WorkerFactory

For the curious: We said that the default WorkerFactory uses reflection to instantiate the right ListenableWorker class. This can fail if our workers class names are minimized by R8 (or ProGuard). To avoid that, WorkManager includes a proguard-rules.pro file that avoids obfuscation of your Worker class names.

出于好奇 :我们说默认的 WorkerFactory 使用反射来实例化正确的 ListenableWorker 类。 如果通过R8(或ProGuard)最小化我们的工人类别名称,则可能会失败。 为避免这种情况,WorkManager包含一个 proguard-rules.pro 文件,该文件避免混淆您的 Worker 类名。

自定义配置和WorkerFactory (Custom configuration and WorkerFactory)

The WorkManager class follows the singleton pattern and it can only be configured before instantiation. This means that if you want a custom configuration, you need to disable the default configuration first.

WorkManager类遵循单例模式 ,并且只能在实例化之前进行配置。 这意味着,如果需要自定义配置,则需要先禁用默认配置。

If you try and initialize WorkManager a second time using the initialize() method an exception (added in v1.0.0) will be thrown. To avoid this, disable the default initialization. You can then configure and initialize WorkManager in your Application’s onCreate method.

如果您尝试使用initialize()方法第二次初始化WorkManager,则将引发异常(添加到v1.0.0中 )。 为避免这种情况,请禁用默认初始化。 然后,您可以在应用程序的onCreate方法中配置和初始化WorkManager。

A newer, better way to initialize WorkManager has been added in v2.1.0. You can use an on-demand initialization by implementing WorkManager’s Configuration.Provider interface in your Application class. Then you just need to get the instance using getInstance(context) and WorkManager will initialize WorkManager using your custom configuration.

v2.1.0中添加了更新,更好的初始化WorkManager的方法。 您可以通过在Application类中实现WorkManager的Configuration.Provider接口来使用按需初始化 。 然后,您只需要使用getInstance(context)获取实例,WorkManager将使用您的自定义配置初始化WorkManager。

可配置参数 (Configurable parameters)

As we already said, you can configure the WorkerFactory used to create the workers, but you can also customize other parameters. The full list of parameters is in WorkManager’s reference guide for the Configuration.Builder. Here I want to call out two additional parameters:

如前所述,您可以配置用于创建工作程序的WorkerFactory ,但也可以自定义其他参数。 参数的完整列表在WorkManager的Configuration.Builder参考指南中。 在这里,我想调用另外两个参数:

  • Logging level

    记录级别
  • JobId range

    JobId范围

Modifying the logging level comes in handy when we need to understand what is going on with WorkManager. We have a documentation page on this topic. You can take a look at the Advanced WorkManager codelab to see how this is implemented in a real sample and what kind of information you can obtain.

当我们需要了解WorkManager发生了什么时,修改日志记录级别非常方便。 我们有关于此主题的文档页面 。 您可以查看Advanced WorkManager代码实验室,以了解如何在实际示例中实现此功能以及可以获取什么样的信息。

We may want to customize the JobId range, if we are using WorkManager as well as the JobScheduler API in our application. In this case you want to avoid using the same JobId range in both places. There’s also a new Lint rule that covers this case introduced in v2.4.0.

如果我们在应用程序中使用WorkManager和JobScheduler API,则可能需要自定义JobId范围。 在这种情况下,您要避免在两个地方使用相同的JobId范围。 还有一个新的Lint规则涵盖了v2.4.0中引入的这种情况。

WorkManager的WorkerFactory (WorkManager’s WorkerFactory)

We already know WorkManager has a default WorkerFactory that uses reflection to find which class to instantiate based on the Worker class name we passed in our WorkRequest.

我们已经知道WorkManager有一个默认的WorkerFactory ,它使用反射根据我们在WorkRequest传递的Worker类名称来实例化要实例化的类。

⚠️ If you create a WorkRequest and then you refactor the app using a different class name for your worker, WorkManager will not be able to find the right class and will throw a ClassNotFoundException.

⚠️ 如果创建 WorkRequest ,然后为工作人员使用不同的类名称来重构应用程序,则WorkManager将无法找到正确的类,并将抛出 ClassNotFoundException

You probably want to add other parameters to your worker’s constructor. Imagine having a worker that expects a reference to a Retrofit service needed to communicate with a remote server:

您可能想将其他参数添加到工作程序的构造函数中。 想象一下,有一个工作人员期望引用与远程服务器进行通信所需的改造服务:

If we make this change to an application it will still compile, but, as soon as we execute it and WorkManager tries to instantiate this CoroutineWorker class, the application will be closed with an exception, complaining that it’s not possible to find the right init method to instantiate:

如果对应用程序进行此更改,它仍会编译,但是,一旦执行它,并且WorkManager尝试实例化此CoroutineWorker类,该应用程序将因异常而关闭,并抱怨找不到正确的init方法。实例化:

Caused by java.lang.NoSuchMethodException: <init> [class android.content.Context, class androidx.work.WorkerParameters]

We need a custom WorkerFactory!

我们需要一个自定义的WorkerFactory

But, not so fast: we already saw that there are few steps involved. Let’s recap what we have to do, then dive into each item details:

但是,速度并不快:我们已经看到其中涉及几个步骤。 让我们回顾一下我们要做的事情,然后深入研究每个项目的详细信息:

  1. Disable the default initialization

    禁用默认初始化
  2. Implement a custom WorkerFactory

    实现一个自定义的WorkerFactory

  3. Create a custom configuration

    创建自定义配置
  4. Initialize WorkManager

    初始化WorkManager

禁用默认初始化 (Disable the default initialization)

As described in WorkManager’s documentation, disabling has to be done in your AndroidManifest.xml file, removing the node that is merged automatically from the WorkManager library by default.

WorkManager文档中所述 ,必须在AndroidManifest.xml文件中进行禁用,默认情况下会从WorkManager库中删除自动合并的节点。

实现一个自定义的WorkerFactory (Implement a custom WorkerFactory)

We now need to write our own factory that creates our worker with the right parameters:

现在,我们需要编写自己的工厂来创建带有正确参数的工人:

创建一个自定义的WorkerConfiguration (Create a custom WorkerConfiguration)

Next, we have to register our factory in our WorkManager’s custom configuration:

接下来,我们必须在WorkManager的自定义配置中注册工厂:

初始化WorkManager (Initialize WorkManager)

This is all you need if you have a single Worker class type in your application. If you have more than one, or you expect to have more in the future, a better solution is to use the DelegatingWorkerFactory introduced in v2.1.

如果您的应用程序中只有一个Worker类类型,那么这就是您所需要的。 如果您有多个,或者希望将来有更多,则更好的解决方案是使用v2.1中引入的DelegatingWorkerFactory

DelegatingWorkerFactory (DelegatingWorkerFactory)

Instead of configuring WorkManager to directly use our factory, we can use a DelegatingWorkerFactory and add to it our own WorkerFactory using its addFactory() method. You can then have multiple factories where each one takes care of one or more workers. Register your factory with the DelegatingWorkerFactory and it will take care to coordinate the multiple factories.

代替将WorkManager配置为直接使用我们的工厂,我们可以使用DelegatingWorkerFactory并使用其addFactory()方法向其添加自己的WorkerFactory 。 然后,您可以拥有多个工厂,每个工厂都照顾一个或多个工人。 在DelegatingWorkerFactory注册您的工厂,这将有助于协调多个工厂。

In this case your factory needs to check if the workerClassName passed as a parameter is something that it knows how to handle. If not, it returns null and the DelegatingWorkerFactory will move to the next registered factory. If none of the registered factories know how to handle a class, it will fall back to the default factory that uses reflection.

在这种情况下,您的工厂需要检查作为参数传递的workerClassName是否知道如何处理。 如果不是,则返回nullDelegatingWorkerFactory将移至下一个注册的工厂。 如果所有注册工厂都不知道如何处理类,则它将退回到使用反射的默认工厂。

Here’s our factory modified to return null if it doesn’t know how to handle a workerClassName:

这是我们的工厂修改后的结果,如果它不知道如何处理workerClassName ,则返回null

Our WorkManager configuration then becomes:

然后,我们的WorkManager配置将变为:

If you have more than one Worker that requires different parameters, you can create a second factory and add it calling addFactory a second time.

如果您有多个需要不同参数的Worker ,则可以创建第二个工厂并再次调用addFactory添加它。

结论 (Conclusions)

WorkManager is a powerful library, able to cover a lot of common use cases with its default configuration. However there are some cases when you need to increase its debugging level or need to pass additional parameters to your worker. In these cases you need a custom configuration.

WorkManager是一个功能强大的库,它的默认配置能够涵盖许多常见的用例。 但是,在某些情况下,您需要提高其调试级别或需要将其他参数传递给工作人员。 在这些情况下,您需要自定义配置。

I hope that this article has given you a good overview of this topic, let me know if you have any questions in the comments or contacting me directly on twitter at pfmaggi@.

希望本文能为您提供一个很好的概述,让我知道您是否有任何疑问或可以通过pfmaggi @在Twitter上直接与我联系。

The next article covers how to use Dagger in a WorkManager custom configuration: “Customizing WorkManager with Dagger”.

下一篇文章介绍如何在WorkManager自定义配置中使用Dagger:“使用Dagger自定义WorkManager ”。

翻译自: https://medium.com/androiddevelopers/customizing-workmanager-fundamentals-fdaa17c46dd2

web 定制工作日历

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值