我正在尝试将 SharedPrefernces 注入(inject) Timer
前台服务类。我在服务类上使用 @EntryPoint
注释。
ApplicationModule 类
@Module
@InstallIn(SingletonComponent::class)
class ApplicationModule() {
@Singleton
@Provides
fun provideSharedPreferences(@ApplicationContext appContext: Context): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(appContext)
}
}
定时器服务类
@EntryPoint
class Timer : Service() {
@Inject
lateinit var preferences: SharedPreferences
...
}
当我运行这段代码时,我得到了这个错误:
@EntryPoint com.beu.coroutineskotlin.Timer 也必须用@InstallIn 注解 [Hilt] 处理未完成。有关详细信息,请参见上面的错误。警告:上一轮创建的类型为“com.beu.coroutineskotlin.App_HiltComponents”的文件将不受注释处理。
最佳答案
我还没有尝试过服务。但是,我认为您必须使用 @AndroidEntryPoint
而不是 @EntryPoint
。 @EntryPoint
用于创建这样的自定义入口点 https://dagger.dev/hilt/entry-points.html
关于android - 如何使用 Hilt 将 SharedPreferences 注入(inject)后台服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66732738/