-1

I need to create an android service for my application which requires the killing of a previous service of the same class when a new startservice method is called.

I need this for loading data from the web to my application. I regularly poll a notifications API and was trying to start a service using the AlarmManager using inexactalarms. I can't seem to destroy it. I wanted a previous instance of a service to be destroyed in case a new one is launched.

I have tried ending the service using stopSelf() and stopService(), both don't seem to work.

  • I tried ending the service using stopSelf() stopService(), don't seem to work. @Alex – Anand Singh Kunwar Jan 16 '15 at 20:58
  • 1
    This is impossible and unusable, considering the architecture of the Android's `Service` class. You can however use the `onStartCommand(...)` of your service which is called every time you supply your service class as a parameter of `startService(...)` method to start new internet request. But you cannot have multiple instances of the same service running, hence, you cannot have multiple instances of the same service interacting – nstosic Jan 16 '15 at 20:59
  • It's not clear why this is needed to download from the internet though. You could try 3rd party free libraries such as OkHttp to get data from internet. – Alexander Kulyakhtin Jan 16 '15 at 21:00
  • I just need to terminate the present instance of the service and start a fresh instance. The data I'm talking about is a JSON object from my web-app's API. @NitroNbg – Anand Singh Kunwar Jan 16 '15 at 21:02
  • 2
    "I just need to terminate the present instance of the service and start a fresh instance" -- repeating yourself does not explain *why* you think that you need this, so that we can help you find a better solution for your problem. – CommonsWare Jan 16 '15 at 21:07
  • I need this because a button has an onClickListener which calls a function in my app. This function in turn starts the service to download data. If the user clicks this button again, the data needs to be download from afresh. This is what I require. @CommonsWare – Anand Singh Kunwar Jan 16 '15 at 21:26
  • 1
    That *still* does not explain why you think that the proper way to do this requires you to "terminate the present instance of the service and start a fresh instance". Certainly there is nothing in your previous comment that would "require" this. An existing service instance is perfectly capable of downloading fresh data. – CommonsWare Jan 16 '15 at 21:49

1 Answers1

2

A service should be used to handle incoming intents, binding to activities and performing background work. You should never have to kill it. In fact, you should be concerned about times when it might be killed because of a low-memory event, etc.

If you think that you must kill it, then you are probably confusing "thread" with "service." A thread might need to be stopped and then killed with a new one spawning, but not a service.

Whatever logic you are currently doing in your Service - try to take it and put it into a thread that you can manage, i.e. "start" "stop" "destroy" (although those particular methods are deprecated)

Then use the Service to manage that thread class based on the intents and bindings it has. In other words, when it receives an intent, perform whatever logic you need in order to kill your thread, and then start another one.

You can also use AsyncTask if you are not comfortable with native threads.

EDIT:

As CommonsWare pointed out, a Service should be delivering value. It should not be running unless needed.

Jim
  • 10,172
  • 1
  • 27
  • 36
  • 1
    A wise suggestion; the only thing I would add is to keep in mind that the `stop()` and `destroy()` methods for the `Thread` class are deprecated. Instead, an interrupt request should be posted to the `Thread` via the `interrupt()` method. – Willis Jan 16 '15 at 21:29
  • yes - I put those in quotes because `interrupt` is one of many ways; I edited my answer to help clarify. thanks! – Jim Jan 16 '15 at 21:35
  • 1
    "You should never have to kill it" -- now, that's much too far in the other extreme. A service should only be running when it is actively delivering value to the user. – CommonsWare Jan 16 '15 at 21:48
  • @CommonsWare - I agree. I emphasized the wrong word! Thanks for pointing that out and I've edited my answer to reflect. – Jim Jan 17 '15 at 18:03
  • 1
    "Generally, Android will take care of that. Once all binders are released and commands processed" -- only if you are using the binding pattern or are using `IntentService`. In a regular `Service`, started by `startService()`, *you* need to stop it, either by calling `stopService()` or having the service itself call `stopSelf()`. – CommonsWare Jan 17 '15 at 18:20
  • I deleted that part. I've working a lot with `IntentService` recently, so it was on my mind and I wasn't thinking about the regular `Service` class – Jim Jan 17 '15 at 20:55