Android Dagger2依赖注入
我是Dagger2的新手,我正在尝试在我的应用程序中使用依赖注入。 我正在使用共享首选项,并认为使用依赖项注入比每次需要使用共享首选项时都获取共享首选项实例更有帮助。 当我在活动和片段上使用它时,它工作得很好,但当我试图在服务或intentservice上使用它时,它不工作 这是我的密码: AppModule:Android Dagger2依赖注入,android,dependency-injection,dagger-2,Android,Dependency Injection,Dagger 2,我是Dagger2的新手,我正在尝试在我的应用程序中使用依赖注入。 我正在使用共享首选项,并认为使用依赖项注入比每次需要使用共享首选项时都获取共享首选项实例更有帮助。 当我在活动和片段上使用它时,它工作得很好,但当我试图在服务或intentservice上使用它时,它不工作 这是我的密码: AppModule: @Module public class AppModule { public final ApplicationClass application; public A
@Module
public class AppModule
{
public final ApplicationClass application;
public AppModule(ApplicationClass application)
{
this.application = application;
}
@Provides @Singleton
Context providesApplicationContext()
{
return this.application;
}
@Provides @Singleton
SharedPreferences providesSharedPreferences()
{
return application.getSharedPreferences(Constants.FILE_NAME,Context.MODE_PRIVATE);
}
}
@Singleton @Component(modules = {AppModule.class})
public interface AppComponent
{
void inject (YourApplicationClass applicationClass);
void inject (BasicIntentService intentService);
}
AppComponent
@Singleton @Component(modules = {AppModule.class})
public interface AppComponent
{
void inject (ApplicationClass applicationClass);
void inject (IntentService intentService);
void inject (Service service);
}
应用程序类
public class ApplicationClass extends Application
{
AppComponent appComponent;
@Override
public void onCreate()
{
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(new
Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
onUncaughtException(t, e);
}
});
appComponent = DaggerAppComponent
.builder()
.appModule(new AppModule(this))
.build();
appComponent.inject(this);
}
public AppComponent getAppComponent()
{
return this.appComponent;
}
private void onUncaughtException(Thread t, Throwable e)
{
e.printStackTrace();
Intent crash= new Intent(getApplicationContext(),Crash.class);
about.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(crash);
}
}
因此,我尝试在IntentService中注入共享首选项,并使用了这些代码行
在my服务(intentservice)的onCreate方法中
但问题是,当我在onHandleIntent
方法中使用这个preferences变量时,应用程序正在崩溃,因为首选项为空。。
那么为什么它不注射呢?适用于遇到此问题的任何人。 正如Vadim Korzun和EpicPandaForce在上述评论中提到的, 我应该在inject方法中指定特定的类 因此,在我的例子中,我的IntentService类被命名为
GeofenceService
在AppComponent接口中,我应该编写
void inject (GeofenceService service);
服务也一样
更新:
@Module
public class AppModule
{
public final ApplicationClass application;
public AppModule(ApplicationClass application)
{
this.application = application;
}
@Provides @Singleton
Context providesApplicationContext()
{
return this.application;
}
@Provides @Singleton
SharedPreferences providesSharedPreferences()
{
return application.getSharedPreferences(Constants.FILE_NAME,Context.MODE_PRIVATE);
}
}
@Singleton @Component(modules = {AppModule.class})
public interface AppComponent
{
void inject (YourApplicationClass applicationClass);
void inject (BasicIntentService intentService);
}
因此,对于所有拥有多个从IntentService继承的服务并希望避免为每个特定服务编写inject方法的人来说。
我建议执行以下步骤:
basictentservice
扩展IntentService
AppComponent
界面中添加inject方法,该方法将basictentservice
作为参数BasicIntentSerive
中,您将有一个受保护的SharedReferences
变量,该变量使用Inject
注释进行注释basictentservice
中,在onCreate
方法中,您将调用这一行代码
((ApplicationClass)getApplication()).getAppComponent().inject(此)IntentService
将扩展basictentservice
,并且您将能够使用SharedReferences
变量AppComponent:
@Module
public class AppModule
{
public final ApplicationClass application;
public AppModule(ApplicationClass application)
{
this.application = application;
}
@Provides @Singleton
Context providesApplicationContext()
{
return this.application;
}
@Provides @Singleton
SharedPreferences providesSharedPreferences()
{
return application.getSharedPreferences(Constants.FILE_NAME,Context.MODE_PRIVATE);
}
}
@Singleton @Component(modules = {AppModule.class})
public interface AppComponent
{
void inject (YourApplicationClass applicationClass);
void inject (BasicIntentService intentService);
}
基本服务
public class BasicIntentService extends IntentService
{
@Inject
protected SharedPreferences sharedPreferences;
@Override
public void onCreate()
{
super.onCreate()
((YourApplicationClass)getApplication()).getAppComponenet().inject(this);
}
}
public class SomeIntentService extends BasicIntentService
{
@Override
public void onCreate()
{
super.onCreate();
}
-----------
@Override
protected void onHandleIntent(@Nullable Intent intent)
{
// some code
if (sharedPreferences.contains(someKey))
{
// some code
}
else
{
// some code
}
}
}
SomeIntentService
public class BasicIntentService extends IntentService
{
@Inject
protected SharedPreferences sharedPreferences;
@Override
public void onCreate()
{
super.onCreate()
((YourApplicationClass)getApplication()).getAppComponenet().inject(this);
}
}
public class SomeIntentService extends BasicIntentService
{
@Override
public void onCreate()
{
super.onCreate();
}
-----------
@Override
protected void onHandleIntent(@Nullable Intent intent)
{
// some code
if (sharedPreferences.contains(someKey))
{
// some code
}
else
{
// some code
}
}
}
对于遇到此问题的任何人。 正如Vadim Korzun和EpicPandaForce在上述评论中提到的, 我应该在inject方法中指定特定的类 因此,在我的例子中,我的IntentService类被命名为
GeofenceService
在AppComponent接口中,我应该编写
void inject (GeofenceService service);
服务也一样
更新:
@Module
public class AppModule
{
public final ApplicationClass application;
public AppModule(ApplicationClass application)
{
this.application = application;
}
@Provides @Singleton
Context providesApplicationContext()
{
return this.application;
}
@Provides @Singleton
SharedPreferences providesSharedPreferences()
{
return application.getSharedPreferences(Constants.FILE_NAME,Context.MODE_PRIVATE);
}
}
@Singleton @Component(modules = {AppModule.class})
public interface AppComponent
{
void inject (YourApplicationClass applicationClass);
void inject (BasicIntentService intentService);
}
因此,对于所有拥有多个从IntentService继承的服务并希望避免为每个特定服务编写inject方法的人来说。
我建议执行以下步骤:
basictentservice
扩展IntentService
AppComponent
界面中添加inject方法,该方法将basictentservice
作为参数BasicIntentSerive
中,您将有一个受保护的SharedReferences
变量,该变量使用Inject
注释进行注释basictentservice
中,在onCreate
方法中,您将调用这一行代码
((ApplicationClass)getApplication()).getAppComponent().inject(此)IntentService
将扩展basictentservice
,并且您将能够使用SharedReferences
变量AppComponent:
@Module
public class AppModule
{
public final ApplicationClass application;
public AppModule(ApplicationClass application)
{
this.application = application;
}
@Provides @Singleton
Context providesApplicationContext()
{
return this.application;
}
@Provides @Singleton
SharedPreferences providesSharedPreferences()
{
return application.getSharedPreferences(Constants.FILE_NAME,Context.MODE_PRIVATE);
}
}
@Singleton @Component(modules = {AppModule.class})
public interface AppComponent
{
void inject (YourApplicationClass applicationClass);
void inject (BasicIntentService intentService);
}
基本服务
public class BasicIntentService extends IntentService
{
@Inject
protected SharedPreferences sharedPreferences;
@Override
public void onCreate()
{
super.onCreate()
((YourApplicationClass)getApplication()).getAppComponenet().inject(this);
}
}
public class SomeIntentService extends BasicIntentService
{
@Override
public void onCreate()
{
super.onCreate();
}
-----------
@Override
protected void onHandleIntent(@Nullable Intent intent)
{
// some code
if (sharedPreferences.contains(someKey))
{
// some code
}
else
{
// some code
}
}
}
SomeIntentService
public class BasicIntentService extends IntentService
{
@Inject
protected SharedPreferences sharedPreferences;
@Override
public void onCreate()
{
super.onCreate()
((YourApplicationClass)getApplication()).getAppComponenet().inject(this);
}
}
public class SomeIntentService extends BasicIntentService
{
@Override
public void onCreate()
{
super.onCreate();
}
-----------
@Override
protected void onHandleIntent(@Nullable Intent intent)
{
// some code
if (sharedPreferences.contains(someKey))
{
// some code
}
else
{
// some code
}
}
}
您不必将上下文和共享首选项注入到IntentService,IntentService已经从上下文继承。您的问题是,您应该在AppComponent中的目标类(inject方法)上使用name,例如:
void inject(ApplicationClass ApplicationClass);无效注入(CustomIntentService intentService);无效注入(SimpleIntentService服务)代码>您需要指定一个特定的具体类,而不是它的父类来注入它。所以你不能只说IntentService
然后注入到任何intent服务中,它将是空的,因为你的类不是IntentService
,而是WhateverService
@VadimKorzun谢谢,正如你所看到的,我的意思是共享首选项并编辑了我的问题。谢谢你澄清我的问题problem@EpicPandaForce也谢谢你!我认为,如果我将IntentService作为参数传递给inject方法,我可以将这个函数用于我的应用程序中继承自IntentService的所有类。谢谢:)您不必向IntentService注入上下文和共享首选项,IntentService已经从上下文继承。您的问题是,您应该在AppComponent中的目标类(inject方法)上使用name,例如:void inject(ApplicationClass ApplicationClass);无效注入(CustomIntentService intentService);无效注入(SimpleIntentService服务)代码>您需要指定一个特定的具体类,而不是它的父类来注入它。所以你不能只说IntentService
然后注入到任何intent服务中,它将是空的,因为你的类不是IntentService
,而是WhateverService
@VadimKorzun谢谢,正如你所看到的,我的意思是共享首选项并编辑了我的问题。谢谢你澄清我的问题problem@EpicPandaForce也谢谢你!我认为如果我将IntentService作为参数传递给inject方法,我可以使用这个函数