rahulserver
rahulserver

Reputation: 11205

Restart a service

I have an android application containing an Activity and a service. I want to make the service start at boot. I would use activity to just give a UI to the user to configure certain params and start the service. I want the service to start at boot time. I know that I can do it this way.

Now I have two questions related to this:

  1. The service would always start at boot. But what if I want to stop it. Can I do it in the activity by simply calling stopService()?(Needed as suppose the user wants to change the params of the running service.)
  2. If I install a newer version of the application, which might have few changes in the service class, will it auto stop the service and replace it with the newer one?

Upvotes: 0

Views: 74

Answers (2)

Jim
Jim

Reputation: 10278

stopService will stop your service, assuming you called the correct service (if you have only 1, then it will work). And this is necessary for exactly your reason for asking, if params change you need to be able to start/stop the service.

Å new version update will automatically stop the service. After JB, the service will not automatically restart until the user opens your app, even on reboot. This is for security reasons.

Upvotes: 1

Aloong
Aloong

Reputation: 1725

Add a BroadcastReceiver in your service, like you listening to the BOOT_COMPLETED broadcast, then you can send a customized broadcast in your visual activity to make the service stop.
In this way you can also restart the service, just finish it and start a new one.

Upvotes: 0

Related Questions