public class SomeClass extends ContextWrapper {
public SomeClass(Context base) {super(base);}
public void someMethod() {// notice how I can use "this" for Context// this works because this class has it's own Context just like an Activity or ServicestartActivity(this, SomeRealActivity.class);
//would require context tooFile cacheDir = getCacheDir();}}
@HiltAndroidAppclass MyApplication : Application() {
@Injectlateinit var dbManager: DBManager
override fun onCreate() {super.onCreate()dbManager.initDB()}}
定义数据库管理器(例如也让它为@Singleton):
@Singletonclass DBManager @Inject constructor(@ApplicationContext private val context: Context) {
fun initDB() {// context is avaiabledatabaseInit(context)}}