AndroidCoder
AndroidCoder

Reputation: 2735

Android JobIntentService - onStartCommand vs onHandleWork

I'm currently extending from Service class for my customized service implementation. As part of Android-O migration, I want to use JobIntentService.

Currenlty all my logic is in service's onStartCommand method.

However, as per JobIntentService I should use onHandleWork method for my logical part.

The official documentation for JobIntentService says that onStartCommand Processes start commands when running as a pre-O service, enqueueing them to be later dispatched in onHandleWork(Intent).

So, my question is do I need to keep both onStartCommand & onHandleWork methods & do I need to write the same logic in both of these methods?

OR

Simply onHandleWork will work?

Please clarify.

Upvotes: 4

Views: 2366

Answers (2)

Gugelhupf
Gugelhupf

Reputation: 970

You need to use the enqueueWork(@NonNull Context context, @NonNull Class cls, int jobId, @NonNull Intent work)

from the JobIntentService to start the Job

See https://developer.android.com/reference/android/support/v4/app/JobIntentService

Upvotes: -1

CommonsWare
CommonsWare

Reputation: 1006944

Currenlty all my logic is in service's onStartCommand method.

It is unclear why you have the service, then. Your service needs to fork a background thread, and that implies creating other classes (e.g., subclasses of Thread, Runnable).

do I need to keep both onStartCommand & onHandleWork methods & do I need to write the same logic in both of these methods?

If you are using JobIntentService, you put everything in onHandleWork(). This is a direct analogue of onHandleIntent() in an IntentService — it will be called on a background thread, and if there is no more work to be done when the method returns, the service can go away.

Upvotes: 3

Related Questions