私は自分のプロジェクトでDagger2リリースのライブラリを使用しています。私は呼び出しています、DaggerでIntentServiceを挿入する方法
今@Module
public class SplashActivityModule {
@Provides
SplashViewModel provideLoginViewModel(TraderRepository traderRepository, Application application, SharedPreferenceUtils sharedPreferenceUtils, AppExecutors appExecutors) {
return new SplashViewModel(traderRepository, application, sharedPreferenceUtils, appExecutors);
}
}
:次にActivityBuilderのように特定の活動と関連モジュールを定義します
@Singleton
@Component(modules = {
AndroidInjectionModule.class, AppModule.class, ActivityBuilder.class })
public interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);
AppComponent build();
}
void inject(TraderApplication traderApplication);
}
:
@Module
public abstract class ActivityBuilder {
@ContributesAndroidInjector(modules = SplashActivityModule.class)
abstract SplashActivity bindSplashActivity();
}
とSplashActivityModuleのように依存関係を提供している私はとAppComponentを定義していますSplashActivityのIntentService(いくつかの設定データをロードする)が、そのIntentServiceの中には、SharePreference、AppExecutersなどの@Inject依存関係はできません。AppM私はのonCreate内部のように注入しようとしています と内部SetupDataService:Update_1 **
@Module
public class AppModule {
@Provides
@Singleton
TraderRepository provideIssueRepository(TraderListRepositoryImpl repository) {
return repository;
}
@Provides
@Singleton
SharedPreferences providesSharedPreferences(Application application) {
return PreferenceManager.getDefaultSharedPreferences(application);
}
}
:oduleは次のように定義されて
public class SetupDataService extends IntentService implements HasServiceInjector {
@Inject
SharedPreferenceUtils mSharedPreferenceUtils;
@Inject
AndroidInjector<Service> fragmentDispatchingAndroidInjector;
@Override
public AndroidInjector<Service> serviceInjector() {
return fragmentDispatchingAndroidInjector;
}
@Override
public void onCreate() {
AndroidInjection.inject(this);
super.onCreate();
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
assert intent != null;
//**update_2 :
if(mSharedPreferenceUtils != null) //still null here after using @binds
{
}
}
}
それは私がしようとした場合でも、私たちは注入することはできません。アプリケーションクラス内にHasServiceInjectorを実装します。それは以下のようにスローされます: '@Provideまたは@Produceなしでサービスを挿入できません'。 ** Update_1はここで終了
..
はどのようにDagger2.11リリースを使用してテントのサービスを注入します。私は定義しようとしました:AppComponentの中でvoid inject(IntentService intentService);
でも、私は依存性を注入中にNULL値を取得しています。
** Update_2:
と私はサービスクラスに注入しようとしていますSharedPreferenceUtilsクラスはonHandleIntentの内側にnullを返します:
@Singleton
public class SharedPreferenceUtils {
//region Private Fields
private SharedPreferences mSharedPreferences;
private final String TAG = SharedPreferenceUtils.class.getSimpleName();
private final Method APPLY_METHOD = findApplyMethod();
//endregion
@Inject
public SharedPreferenceUtils(SharedPreferences mSharedPreferences) {
this.mSharedPreferences = mSharedPreferences;
}
}
** Update_2はここで終了..
ここにすべてのヘルプ..
ありがとう。
あなたは私たちにIntentServiceを表示することができますか?どこでそれを行うには、あなたが私たちを示した 'inject'メソッドを呼び出すと、https://github.com/google/dagger/blob/master/java/dagger(あなたは[ContributesAndroidInjector' @サービスを注入するために'使用]と考えられてい/android/AndroidInjection.java#L127)? –
@JeffBowman * Update_1の編集を確認してください。ありがとう。 – prdp89