[英]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.