Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 726
Amrut Avatar asked Oct 20 '25 15:10

Amrut


1 Answers

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.

like image 87
CommonsWare Avatar answered Oct 22 '25 04:10

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!