3

I want to handle IntentService onStop event. IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate but I have not found onStop method. It has onDestroy() but actually onDestroy() method can not be called.

onDestroy is called only when system is low on resources(memory, cpu time and so on) and makes a decision to kill your activity/application or when somebody calls finish() on your activity.

Can I add my code in onStop IntentService time ?

Prachi
  • 2,559
  • 4
  • 25
  • 37
sytolk
  • 7,223
  • 3
  • 25
  • 38

2 Answers2

1

You can use stopSelf() to stop service. To perform task before service stops you can write your custom callback/ broadcast after your thread to notify UI.

Prachi
  • 2,559
  • 4
  • 25
  • 37
  • Yes its look like writing my broadcast after service execution is only possible. But this is Download IntenetService with many download threads I need to handle event and send callback/broadcast after all downloads completed. – sytolk Jul 08 '15 at 13:20
1

If you are subclassing IntentService you should be using onHandleIntent(Intent intent) for the lifecycle of your service. Your service might be moving to onDestroy quickly becuase you do not have code inside of onHandleIntent.

Also it might aways move to onDestroy quickly because IntentService is auto threaded for you and might just launch the worker thread which calls onHandleIntent and move to onDestroy

Vaisakh N
  • 780
  • 1
  • 11
  • 27
  • The question is how can handle onStop IntentService ? service might be moving to onDestroy ? I`m not sure.. – sytolk Jul 09 '15 at 11:51