Android: accessing switch values ​​from a service

In my Android app, the service runs in the background and writes GPS readings to the database . The user sees the activity that represents him using the switches . I would also like to register the currently selected radio button in the database.

How can I access the switch object from the Service?

+4
source share
1 answer

One easy way to do this (if your service is running in the same process as Activity) is to use the general settings file. The action can be written to the settings file whenever the switch is changed, and the Service can either register the listener in the settings or check the status when writing to the database.

A more complicated way to avoid preferences would be to start the service with Context.startService () (so that it remains running even if the activity does not exist), and then for Activity to call Context.bindService () (start the service if not running) to return an IBinder stub with which it can interact. For this approach, I recommend that you look at the Service APIs .

+3
source

All Articles