簡體   English   中英

如何使用 Hilt 將 SharedPreferences 注入后台服務

[英]How to inject SharedPreferences to Background service using Hilt

我正在嘗試將 SharedPrefernces 注入Timer前台服務 class。 我在服務 class 上使用@EntryPoint注釋。

ApplicationModule class

@Module
@InstallIn(SingletonComponent::class)
class ApplicationModule() {

@Singleton
@Provides
fun provideSharedPreferences(@ApplicationContext appContext: Context): SharedPreferences {
      return PreferenceManager.getDefaultSharedPreferences(appContext)
  }
}

Timer service class

@EntryPoint
class Timer : Service() {

    @Inject
    lateinit var preferences: SharedPreferences
    ...
    }

當我運行此代碼時,我收到此錯誤: @EntryPoint com.beu.coroutineskotlin.Timer must also be annotated with @InstallIn [Hilt] Processing did not complete. See error above for details.warning: File for type 'com.beu.coroutineskotlin.App_HiltComponents' created in the last round will not be subject to annotation processing. @EntryPoint com.beu.coroutineskotlin.Timer must also be annotated with @InstallIn [Hilt] Processing did not complete. See error above for details.warning: File for type 'com.beu.coroutineskotlin.App_HiltComponents' created in the last round will not be subject to annotation processing.

我還沒有嘗試過服務。 但是,我認為您必須使用@AndroidEntryPoint而不是@EntryPoint @EntryPoint用於創建像這樣的自定義入口點https://dagger.dev/hilt/entry-points.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM