我正在尝试创建Dagger提供的用于注入共享首选项的服务,现在具有以下类:
StorageService.java (SharedPreferences的包装,现在具有静态方法,因为我仍然没有完成重构)
public class StorageService {
private final Context context;
private enum Keys {
USER_NAME("userName"), TELEPHONE("telephone"), EMAIL("email");
private final String key;
Keys(String key){
this.key = key;
}
public String getKey(){
return key;
}
}
public StorageService(Context context){
this.context = context;
}
public static String getUserName(Context context){
return getString(context, Keys.USER_NAME, "");
}
public static void setUserName(Context context, String userName){
putString(context, Keys.USER_NAME, userName);
}
public static String getUserEmail(Context context){
return getString(context, Keys.EMAIL, "");
}
public static void setUserEmail(Context context, String email){
putString(context, Keys.EMAIL, email);
}
public static String getTelephone(Context context){
return getString(context, Keys.TELEPHONE, "");
}
public static void setTelephone(Context context, String telephone){
putString(context, Keys.TELEPHONE, telephone);
}
private static SharedPreferences.Editor getEditor(Context context){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
return editor;
}
private static String getString(Context context, Keys key, String defaultValue){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
return sharedPreferences.getString(key.getKey(), defaultValue);
}
private static void putString(Context context, Keys key, String value){
SharedPreferences.Editor editor = getEditor(context);
editor.putString(key.getKey(), value);
editor.apply();
}
private static Boolean getBoolean(Context context, Keys key, Boolean defaultValue){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
return sharedPreferences.getBoolean(key.getKey(), defaultValue);
}
private static void putBoolean(Context context, Keys key, Boolean value){
SharedPreferences.Editor editor = getEditor(context);
editor.putBoolean(key.getKey(), value);
editor.apply();
}
}
StorageServiceComponent.java
@Singleton
@Component(modules = { StorageServiceModule.class })
public interface StorageServiceComponent {
void inject(BaseActivity baseActivity);
}
StorageServiceModule.java
@Module
public class StorageServiceModule {
@Provides
@Singleton
public StorageService providesStorageService(Context context) {
return new StorageService(context);
}
}
MyApplication.java
public class MyApplication extends Application {
private StorageServiceComponent storageServiceComponent;
@Override public void onCreate() {
super.onCreate();
initializeInjector();
}
private void initializeInjector() {
storageServiceComponent = DaggerStorageServiceComponent.builder()
.storageServiceModule(new StorageServiceModule())
.build();
}
public StorageServiceComponent getStorageServiceComponent() {
return storageServiceComponent;
}
}
BaseActivty.java
public class BaseActivity extends AppCompatActivity {
@Inject
StorageServiceModule storageServiceModule;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initializeDagger();
Fabric.with(this, new Crashlytics());
Toast.makeText(this, getStorageService().toString(), Toast.LENGTH_SHORT).show();
}
private void initializeDagger() {
MyApplication application = (MyApplication) getApplication();
application.getStorageServiceComponent().inject(this);
}
protected StorageService getStorageService() {
return storageServiceModule.providesStorageService(getApplicationContext());
}
}
build.gradle
//Dagger 2
def dagger_version = "2.2"
implementation "com.google.dagger:dagger:$dagger_version"
annotationProcessor "com.google.dagger:dagger-compiler:$dagger_version"
当我尝试导入DaggerStorageServiceComponent时遇到以下问题。
无法解析符号'DaggerStorageServiceComponent'
我清理了项目并进行了重建,但是当我完成项目时却什么也没发生。