简体   繁体   English

在Dagger2中将依赖项注入后台服务

[英]Injecting dependencies into background service in Dagger2

I have Shared Preferences as a dagger singleton component. 我有Shared Preferences作为匕首单身组件。 I need to inject it into background services like FirebaseInstanceService . 我需要将它注入后台服务,如FirebaseInstanceService Here's my attempt: 这是我的尝试:

public class InstanceIDListenerService extends FirebaseInstanceIdService {
    @Inject
    Preferences preferences;

    @Override
    public void onTokenRefresh() {
        ((MyApp) getApplication()).getSingletonComponent().inject(this);
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        preferences.setFcmToken(refreshedToken);

    }
}

It is used in this way: 它以这种方式使用:

   <service android:name="com.fcm.InstanceIDListenerService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
   </service>

Should I use ((MyApp) getApplication()).getSingletonComponent().inject(this); 我应该使用((MyApp) getApplication()).getSingletonComponent().inject(this); in the onTokenRefresh listener? onTokenRefresh监听器? Is this the correct listener to inject dependencies? 这是注入依赖项的正确侦听器吗?

I know this question is old, but I've just been hitting my head on this over the last few hours, and found the solution. 我知道这个问题已经过时了,但我在过去的几个小时里一直在努力,并找到了解决方案。

With the new Dagger2 version, you can now have your application implement the HasServiceInjector interface, which lets you inject stuff into services. 使用新的Dagger2版本,您现在可以让您的应用程序实现HasServiceInjector接口,该接口允许您将内容注入服务。

A simple example: 一个简单的例子:

1) Create your services module: 1)创建您的服务模块:

@Module
abstract class ServicesModule {

  @ContributesAndroidInjector
  abstract SomeService ProvideSomeService();
}

2) Add it to your App component: 2)将其添加到您的App组件:

@Component(modules = {
  AndroidSupportInjectionModule.class,
  AppModule.class,
  ActivitiesModule.class,
  ServicesModule.class
})
public interface AppComponent {

  @Component.Builder
  interface Builder {
    @BindsInstance
    Builder application(App application);

    AppComponent build();
  }

  void inject(App app);
}

3) Have your application implement the said interface: 3)让您的应用程序实现所述接口:

public class App extends Application implements HasActivityInjector, HasServiceInjector {

  @Inject
  DispatchingAndroidInjector<Activity> activityInjector;
  @Inject
  DispatchingAndroidInjector<Service> serviceInjector;

  @Override
  public void onCreate() {
    super.onCreate();
    AppInjector.init(this);
  }

  @Override
  public AndroidInjector<Activity> activityInjector() {
    return activityInjector;
  }

  @Override
  public AndroidInjector<Service> serviceInjector() {
    return serviceInjector;
  }
}

4) Finally, inject your service: 4)最后,注入您的服务:

public class SomeService extends Service {

  @Inject
  SomeDependency dependency;

  @Override
  public void onCreate() {
    AndroidInjection.inject(this);
    super.onCreate();
  }

  // Do things with your dependencies

}

I'm using a Service in the example, but my actual use case was with FirebaseInstanceIdService as well. 我在示例中使用了一个服务,但我的实际用例也是使用FirebaseInstanceIdService。 And this worked. 这很有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM