WorkManager learning four

I. Introduction

This article WorkManagerdocuments the advanced use of - custom configuration and initialization.

2. Initialization

The previous usage will WorkManagerinitialize when the program starts. If the program is only used at a certain time WorkManager, it will cause unnecessary performance loss. So on-demand initialization can be used here.

1. Remove the default initialization configuration

This article only records WorkManager2.6the way after

a. Remove additional startupfunctions

Since the function WorkManager2.6is integrated by default later startup, if the function is not used in the application, it can be removed in the following way

 <!-- If you want to disable android.startup completely. -->
 <provider
    android:name="androidx.startup.InitializationProvider"
    android:authorities="${applicationId}.androidx-startup"
    tools:node="remove">
 </provider>

b. Enter and remove workmanagerfunction

If you just want to remove WorkManagerthe function, you can use the following way

 <provider
    android:name="androidx.startup.InitializationProvider"
    android:authorities="${applicationId}.androidx-startup"
    android:exported="false"
    tools:node="merge">
    <!-- If you are using androidx.startup to initialize other components -->
    <meta-data
        android:name="androidx.work.WorkManagerInitializer"
        android:value="androidx.startup"
        tools:node="remove" />
 </provider>

3. Custom configuration

After the default configuration is removed, a custom configuration is required. The relevant code examples are as follows:

class MyApplication() : Application(), Configuration.Provider {
    
    
     override fun getWorkManagerConfiguration() =
           Configuration.Builder()
                .setMinimumLoggingLevel(android.util.Log.INFO)
                .build()
}

This series ends here for the time being, and the rest of the functions will be updated again if they are used in work

4. Reference link

  1. Custom WorkManager configuration and initialization | Android Developers | Android Developers

Guess you like

Origin blog.csdn.net/Mr_Tony/article/details/125866761