[英]Android Dagger2 Dependency Injection
I'm new to Dagger2 and I'm trying to use dependency injection in my application. 我是Dagger2的新手,正在尝试在应用程序中使用依赖项注入。 I'm using shared preferences and thought it will be more helpful to use dependency injection instead of getting an instance of shared prefrences each time I need to use it.
我正在使用共享首选项,并认为使用依赖注入将比每次需要使用共享首选项实例更有用。 It works fine when I'm using it on activities and fragments but when I'm trying to use it on service or intentservice it doesn't work.
当我在活动和片段上使用它时,它工作正常,但是当我尝试在服务或intentservice上使用它时,它不起作用。
Here is my code: 这是我的代码:
AppModule: 的AppModule:
@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);
}
}
AppComponent AppComponent
@Singleton @Component(modules = {AppModule.class})
public interface AppComponent
{
void inject (ApplicationClass applicationClass);
void inject (IntentService intentService);
void inject (Service service);
}
ApplicationClass ApplicationClass
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);
}
}
So I tried to inject the shared preferences in IntentService and I used these lines of code inside the onCreate method of the my service (intentservice) 因此,我尝试在IntentService中注入共享首选项,并在我的服务(intentservice)的onCreate方法中使用了以下代码行
@Inject
SharedPreferences preferences;
@Override
public void onCreate()
{
super.onCreate();
((ApplicationClass)getApplication()).getAppComponent().inject(this);
}
But the problem is when I use this preferences variable in the onHandleIntent
method, the application is crashing because the preferences is null.. So why it doesn't inject? 但是问题是当我在
onHandleIntent
方法中使用此首选项变量时,由于首选项为null,应用程序崩溃了。那么为什么它不注入?
For anyone who encountered this problem. 对于任何遇到此问题的人。 As Vadim Korzun and EpicPandaForce mentioned in their comments above, I should specify the specific class in the inject method.
正如Vadim Korzun和EpicPandaForce在上面的评论中提到的那样,我应该在inject方法中指定特定的类。
So in my case my IntentService class was named GeofenceService
因此,在我的案例中,我的IntentService类被命名为
GeofenceService
and inside the AppComponent interface I should write 在AppComponent接口中,我应该写
void inject (GeofenceService service);
Same thing for Service
Service
同样
UPDATE: 更新:
So for all the people who have multiple Services that inherit from IntentService and want to save themselves from writing inject method for each Specific service. 因此,对于拥有多个从IntentService继承并希望自己避免为每个特定服务编写注入方法的人而言。 I would suggest to do the steps below:
我建议执行以下步骤:
BasicIntentService
that extends IntentService
BasicIntentService
扩展 IntentService
AppComponent
interface add the inject method which take as parameter you BasicIntentService
. AppComponent
接口中,添加将BasicIntentService
作为参数的inject方法。 BasicIntentSerive
, you will have a protected SharedPrefrences
variable annotated with the Inject
annotation. BasicIntentSerive
,您将拥有一个protected SharedPrefrences
变量,该变量带有Inject
注释。 Still, in your BasicIntentService
, inside the onCreate
method you will call this line of code 仍然,在
BasicIntentService
的onCreate
方法中,您将调用以下代码行
((ApplicationClass)getApplication()).getAppComponent().inject(this); 。((ApplicationClass)getApplication())getAppComponent()注入(本);
Now each IntentService
that you will create will extends the BasicIntentService
and you will be able to use the SharedPreferences
variable. 现在,您将创建的每个
IntentService
都将扩展 BasicIntentService
并且您将能够使用SharedPreferences
变量。
AppComponent: AppComponent:
@Singleton @Component(modules = {AppModule.class})
public interface AppComponent
{
void inject (YourApplicationClass applicationClass);
void inject (BasicIntentService intentService);
}
BasicIntentService BasicIntentService
public class BasicIntentService extends IntentService
{
@Inject
protected SharedPreferences sharedPreferences;
@Override
public void onCreate()
{
super.onCreate()
((YourApplicationClass)getApplication()).getAppComponenet().inject(this);
}
}
SomeIntentService SomeIntentService
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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.