0

Is the service the only thing that runs when the service is called? What I mean is that when an application runs, it has a init kind of function, that loads helper services, databases, IoC. In iOS the app delegate is called, so that you can init your helper services? Does this also happen for Android? I have tried and it doesn't seem to. Can someone confirm this behavior. I might be missing something

Pulkit Sethi
  • 1,325
  • 1
  • 14
  • 22

1 Answers1

0

I suggest taking a look at the Android documentation here. They do a good job explaining the workings of a service in Android.

If you're creating a plain old service then it will have an onCreate() method you can override. This will get called when your service is first instantiated. If I understand your question correctly I believe this is what you're looking for. You can initialize whatever your service needs here.

An alternative is onStartCommand which you can use to do startup tasks also. Up to your particular need.

UPDATE: Following clarification on question. As best I know you would setup any initialization logic from within the onStartJob. That said, JobService extends Service so you still have access to onCreate.

enter image description here

In theory (I haven't tested this) you could call your init logic here to be called when the service is first setup.

An alternative I've used in the past is a library called Android-Job created by the team in Evernote. For our use case we were able to initialize and pass directly to the job. It has the added bonus of being backward compatible by deciding what scheduling logic to use based off the users Android version.

Mark OB
  • 492
  • 1
  • 7
  • 15