Such a question, in the project I use the method to test the service, I rendered it into a separate class with the annotation @Module:

@Module public class IsMyServiceRunning { Context context; public IsMyServiceRunning(Class<?> serviceClass,Context context) { isMyServiceRunnin(serviceClass); this.context = context; } public boolean isMyServiceRunnin(Class<?> serviceClass) { ActivityManager manager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); for (ActivityManager.RunningServiceInfo service : manager .getRunningServices(Integer.MAX_VALUE)) { if (serviceClass.getName().equals(service.service.getClassName())) { return true; } } return false; } } 

In the main activation I write:

 public class MainActivity extends Activity implements SurfaceHolder.Callback { @Inject IsMyServiceRunning isMyServiceRunning; 

Created a class

  @Module public class AndroidModule { Context context; public AndroidModule(Context context) { this.context = context; } @SuppressLint("ServiceCast") @Provides @Singleton NotificationManagerCompat manager (Context context) { return (NotificationManagerCompat) context.getSystemService(Context.NOTIFICATION_SERVICE); } @Singleton IsMyServiceRunning isMyServiceRunning(Context context){ return ;//не знаю как правильно возвращать,а главное что именно } } 

I cannot check the performance at this stage because many parts are not implemented, so I’m clarifying whether I use Dependency Injection correctly in this example? Have I implemented everything?

UPDATE:

  @Module public class IsMyServiceRunning { Context context; public IsMyServiceRunning(Context context) { this.context = context; } public boolean isMyServiceRunnin(Class<?> serviceClass) { ActivityManager manager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); for (ActivityManager.RunningServiceInfo service : manager .getRunningServices(Integer.MAX_VALUE)) { if (serviceClass.getName().equals(service.service.getClassName())) { return true; } } return false; } } 

Module:

  @Module public class AndroidModule { Context context; public AndroidModule(Context context) { this.context = context; } @SuppressLint("ServiceCast") @Provides @Singleton NotificationManagerCompat manager (Context context) { return (NotificationManagerCompat) context.getSystemService(Context.NOTIFICATION_SERVICE); } @Singleton IsMyServiceRunning isMyServiceRunning(Class<?> serviceClass){ return isMyServiceRunning(serviceClass); } } 

Mainactivity:

  @Inject IsMyServiceRunning isMyServiceRunning; 
  • No, not right. It is necessary to create a separate class-module in which there will be a method returning an IsMyServiceRunning object. Either the constructor of the IsMyServiceRunning class IsMyServiceRunning be annotated with Inject, but then you need a module that can provide context. - temq
  • `@Singleton IsMyServiceRunning isMyServiceRunning (Context context) {return; } `@ temq not sure what I wrote - upward
  • Show the full code, now nothing is clear. - temq
  • @ temq updated the full code - upward
  • Well, create a new IsMyServiceRunning object there and return it. The only thing that confuses me is the constructor of this class, namely the presence of a useless call to the isMyServiceRunnin method for which you pass the Class parameter to the constructor. I think it can be removed. - temq

1 answer 1

In Dagger terminology, there are such things as component and module.

A module is the part that is responsible for creating the objects that a particular component of the application requires.

A component is a bridge between modules and classes that require certain dependencies.

Based on this, you need to create a module:

 @Module public class AndroidModule { private final Context context; public AndroidModule(Context context) { this.context = context; } @Singleton @Provide IsMyServiceRunning isMyServiceRunning(){ return new IsMyServiceRunning(context); } // остальной код создания нужных объектов } 

Component:

 @Component(modules = AndroidModule.class) @Singleton public interface ApplicationComponent { void inject(MainActivity activity) } 

And further in the activity you can ask the Dagger to provide the necessary dependency:

 public class MainActivity extends Activity implements SurfaceHolder.Callback { @Inject IsMyServiceRunning isMyServiceRunning; public void onCreate(Bundle savedInstanse) { super.onCreate(savedInstance); ApplicationComponent component = // где то инитим и получаем компонент component.inject(this); } } 

UPD: the initialization code of the components may differ, and depends on the names of the components and the modules they require, but in this example it would look something like this:

 ApplicationComponent component = new DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(application)) .build() 

In general, a sufficient number of articles on Dagger has already been written on the same habr , I advise you to read.

  • ApplicationComponent component = // somewhere where we get the component ` component.inject(this); I did not understand that after = indicate - upward
  • @upward code that initializes the component - temq
  • @upward updated answer - temq