if I understand your request you wanted to create a service that runs in background and to start it at system start :
In manifest add this :
<receiver
android:name=".StartSysytemReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
create your BroadcastReceiver :
public class StartSysytemReceiver extends BroadcastReceiver {
private static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
private static final String QUICKBOOT_POWERON = "android.intent.action.QUICKBOOT_POWERON";
@Override
public void onReceive(Context context, Intent intent) {
if (BOOT_COMPLETED.equals(intent.getAction()) ||
QUICKBOOT_POWERON.equals(intent.getAction())) {
//schedule your background service
scheduleJob(context);
}
}
}
//schedule
public void scheduleJob(Context context) {
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
RetryStrategy retryStrategy = dispatcher.newRetryStrategy(2, 300, 1800);
Builder jobBuilder = dispatcher.newJobBuilder().setService(BackgroundJobService.class)
.setTag("scheduleJob_112").setReplaceCurrent(true)
.setRecurring(false).setLifetime(2)
.setRetryStrategy(retryStrategy).setConstraints(new int[]{2});
Job myJob = jobBuilder.build();
dispatcher.mustSchedule(myJob);
}
//backgroundJobService
public class BackgroundJobService extends JobService {
private static final String TAG = "BackgroundJobService";
boolean isWorking = false;
boolean jobCancelled = false;
public BackgroundJobService () {
}
public boolean onStartJob(JobParameters jobParameters) {
this.isWorking = true;
this.doWork(jobParameters);
return this.isWorking;
}
private void doWork(JobParameters jobParameters) {
if (!this.jobCancelled) {
//callYour service her
this.getBaseContext().startService(new Intent(this.getBaseContext(), ServiceToCall.class));
this.isWorking = false;
this.jobFinished(jobParameters, true);
}
}
public boolean onStopJob(JobParameters jobParameters) {
this.jobCancelled = true;
boolean needsReschedule = this.isWorking;
this.jobFinished(jobParameters, this.isWorking);
return needsReschedule;
}
}