Q:

How can I restart a service in android programatically?

In Android I have an application with different activities and a service. The activities are for direct user interaction, in which the user can change data, update data, entry data, whatever. 
The application also has a (background) service running, using the data the user has entered (stock data). The service regulary checks the stock prices  (e.g. every 12 hours) and gives the user a notification, if some conditions match. 
However, as an user I want to be able to change settings of this service. I start the application, and can enter an update interval in one of the application's activity which is stored in a SharedPreference. I am able to change the value, for example, to have the service check the stock data every 3 hours instead.
Do I then have to 'restart' the service? As I understand 'services' in an operating system, the service runs until stopped by some event. But if the service is not stopped, the service does not 'notice' that the update interval has changed! So, in my activity from my main application I need some way to 'restart' the service, so it checks the stock prices in 3 hours, and not in 12 hours!
Any idea how to accomplish this? A google search did not really help. Maybe I looked for the wrong keywords...?

A:

A simple solution to this since your service runs in the same process as your application would be to register an OnSharedPreferenceChangeListener in your service's onCreate method to the same SharedPreferences that the activity is updating.
Then every time a preference changes onSharedPreferenceChanged(...) will be called in your service. You can check when the update interval preference in particular changes and update your service accordingly without ever having to restart it.

