Here is the simplest solution that I could handle.
First create the BaseJob class. This will be the goal of the injection:
public abstract class BaseJob extends Job {
He declared abstract , so there is no need to override any abstract methods declared in the Job class. Instead, methods will be overridden in jobs that inherit from BaseJob .
Create a task that inherits from BaseJob . Any fields entered in BaseJob are available for use:
public class MyActualJob extends BaseJob { public static final int PRIORITY = 1; public MyActualJob() { super(new Params(PRIORITY).requireNetwork().persist()); } @Override public void onAdded() {
Finally, to make sure everything is connected, add a DependencyInjector to the JobManager when you create it. This introduces BaseJob into the job:
DependencyInjector dependencyInjector = new DependencyInjector() { @Override public void inject(Job job) {
Why not skip using BaseJob and paste directly into MyActualJob ? This will work, however, if there are several tasks that are the purpose of the injection, I believe that you will need to use instanceof to check which work was created, and give Job correct class when creating the DependencyInjector :
DependencyInjector dependencyInjector = new DependencyInjector() { @Override public void inject(Job job) { if (job instanceof MyActualJob) { ((MyApplication) app).component().inject((MyActualJob) job); } else if (job instanceof MyRealJob) { ((MyApplication) app).component().inject((MyRealJob) job); } else if (job instanceof MyBetterJob) { ((MyApplication) app).component().inject((MyBetterJob) job); } } };
In my case, most, if not all tasks, need access to the same global objects, so it cleans up to a subclass of BaseJob and uses it as the sole purpose of the injection.