简体   繁体   中英

WorkManager custom initialization complains about WorkManager#initialize

With: implementation "androidx.work:work-runtime:2.3.4"

I'm trying to implement custom initialization to enable more verbose logging according to these instructions .

The first snippet has one line bad: return Configuration.Builder() needs to be return new Configuration.Builder() .

I extended Application as shown like this:

class MyApplication extends Application implements Configuration.Provider {
    @Override
    public Configuration getWorkManagerConfiguration() {
        return new Configuration.Builder()
                .setMinimumLoggingLevel(android.util.Log.INFO)
                .build();
    }
}

I initiate the worker like this:

WorkManager.getInstance(getApplicationContext());

And yet even after all of that I get the application to crash with this complaint:

WorkManager is not initialized properly. You have explicitly disabled WorkManagerInitializer in your manifest, have not manually called WorkManager#initialize at this point, and your Application does not implement Configuration.Provider.

Even though the instructions states:

You do not need to call WorkManager.initialize() yourself

Please refer to this link: https://developer.android.com/topic/libraries/architecture/workmanager/advanced/custom-configuration#custom

OR

Initialize your custom WorkManager like this:

// provide custom configuration
val myConfig = Configuration.Builder()
    .setMinimumLoggingLevel(android.util.Log.INFO)
    .build()

// initialize WorkManager
WorkManager.initialize(this, myConfig)

You need to implement the Configuration.Provider interface in your Application class to provide a custom configuration using WorkManager's on-demand initialization .

This is explained with some sample code in the Advanced WorkManager codelab :

class BlurApplication : Application(), Configuration.Provider {

    override fun getWorkManagerConfiguration(): Configuration = 

        Configuration.Builder()
                     .setMinimumLoggingLevel(android.util.Log.DEBUG)
                     .build()
...
}

Please be sure to have this Application class included in you AndroidManifest.xml :

    <application
        android:name=".BlurApplication"

Doing so, the first time you request WorkManager instance with getInstance(context) the library will be initialized with your custom configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM