Google は、黒人コミュニティに対する人種平等の促進に取り組んでいます。取り組みを見る

WorkerInject

@Target([AnnotationTarget.CONSTRUCTOR]) class WorkerInject
androidx.hilt.work.WorkerInject

Identifies a androidx.work.ListenableWorker's constructor for injection.

Similar to javax.inject.Inject, a Worker containing a constructor annotated with WorkerInject will have its dependencies defined in the constructor parameters injected by Dagger's Hilt. The Worker will be available for creation by the androidx.hilt.work.HiltWorkerFactory that should be set in WorkManager's configuration via androidx.work.Configuration.Builder#setWorkerFactory(androidx.work.WorkerFactory).

Example:

public class UploadWorker extends Worker {
      @WorkerInject
      public UploadWorker(@Assisted Context context, @Assisted WorkerParameters params,
              HttpClient httpClient) {
          // ...
      }
  }
  
@HiltAndroidApp
  public class MyApplication extends Application implements Configuration.Provider {
      @Inject HiltWorkerFactory workerFactory;
 
      @Override
      public Configuration getWorkManagerConfiguration() {
          return Configuration.Builder()
                  .setWorkerFactory(workerFactory)
                  .build();
      }
  }
  

Only one constructor in the Worker must be annotated with WorkerInject. The constructor must define parameters for a androidx.hilt.Assisted-annotated Context and a androidx.hilt.Assisted-annotated WorkerParameters along with any other dependencies. Both the Context and WorkerParameters must not be a type param of javax.inject.Provider nor dagger.Lazy and must not be qualified.

Only dependencies available in the dagger.hilt.android.components.ApplicationComponent can be injected into the Worker.

Summary

Public constructors

Identifies a androidx.work.ListenableWorker's constructor for injection.

Public constructors

<init>

WorkerInject()

Identifies a androidx.work.ListenableWorker's constructor for injection.

Similar to javax.inject.Inject, a Worker containing a constructor annotated with WorkerInject will have its dependencies defined in the constructor parameters injected by Dagger's Hilt. The Worker will be available for creation by the androidx.hilt.work.HiltWorkerFactory that should be set in WorkManager's configuration via androidx.work.Configuration.Builder#setWorkerFactory(androidx.work.WorkerFactory).

Example:

public class UploadWorker extends Worker {
      @WorkerInject
      public UploadWorker(@Assisted Context context, @Assisted WorkerParameters params,
              HttpClient httpClient) {
          // ...
      }
  }
  
@HiltAndroidApp
  public class MyApplication extends Application implements Configuration.Provider {
      @Inject HiltWorkerFactory workerFactory;
 
      @Override
      public Configuration getWorkManagerConfiguration() {
          return Configuration.Builder()
                  .setWorkerFactory(workerFactory)
                  .build();
      }
  }
  

Only one constructor in the Worker must be annotated with WorkerInject. The constructor must define parameters for a androidx.hilt.Assisted-annotated Context and a androidx.hilt.Assisted-annotated WorkerParameters along with any other dependencies. Both the Context and WorkerParameters must not be a type param of javax.inject.Provider nor dagger.Lazy and must not be qualified.

Only dependencies available in the dagger.hilt.android.components.ApplicationComponent can be injected into the Worker.