I need to be able to change the value of an Integer in a service while it is running, using an activity. How should I implement this? Any help or advice is appreciated.
2 Answers
One thing you can do is potentially add a broadcast receiver in your service that you communicate with through your activity.
Here is the communication:
From the activity send a broadcast
Have your BroadcastReceiver (potentially in your service) listen to this broadcast from your activity and when it receives it, just update a member variable found in your service.

- 13,423
- 6
- 32
- 45
-
Would that restart the service? Would you mind adding some pseudo or actual code to show me how to implement it? Thanks – David Flanagan Jan 15 '12 at 19:40
-
The service would have to be running already in order for this to work. – JoxTraex Jan 16 '12 at 01:00
-
Binding is the standard way to expose a rich API towards a service, but calling startService again with a different intent to change what the service is doing is also possible. The worst alternative would be to send broadcasts from the client activity to the service. – Feb 09 '12 at 07:19
-
Either or, it doesn't really matter, but binding is only available as an option if you really want to keep track of that object. Often times you don't care and would rather handle the intent through a simple broadcasts. I dont see any cons to doing iit the way I say. It will work and it will not cause a problem with the frameworks. So you're "worst" alternative is subjective. Now there are other ways, like a specific protocol that is used for activity communication, I haven't used it before, but its somewhere in the documentation. There are multiple ways to do it.. really @bergsell – JoxTraex Feb 09 '12 at 07:24
Assuming that you have a service that you have started with startService() from your client to do some work (e.g. start navigation) in the background for a longer period of time, and then the client wants to change the operation of the service in some way (e.g. use gps). Then I would recommend you to make it possible to bind to the service as well (i.e. it will be possible to start the service and bind to it).
Binding to a service is the default way to get access to a robust API towards a service. And, creating a local service (if the client and the service are running within the same process) is really simple.
http://developer.android.com/guide/topics/fundamentals/bound-services.html#Binder
(A second alternative would be to call startService again with new parameters, but if that is better than binding to the service depends on the actual use case.)
Creating even a simple API using intents is more error prone, and you have no idea if the service gets killed in-between calls (in a low memory situation) since the client will not be notified about the service status unless you bind to it. And, as always, there is no guarantee that a started service will run forever. Assume that your service will be killed; it is just a matter of how long it will take.