I am a bit new to Android. What I need to do is send data back and forth between an activity and a service locally. The information sent is sensitive and must not be able to be picked up by other apps. This excludes using broadcast and the onBind() function if I understand things correctly?
So the activity needs to send some string parameters to the service. Also it somehow needs to tell the service which activity started it so the service can reply to the correct class since many different activities will use this service. Once the service has fetched the information via a http request it is suppose to send that data (just a long string which will later be parsed) back to the activity that started it. How do I go about doing this? Using static variables/functions is not an option since again many actives will be using this service. Sure it would be possible to use a static array to hold the classes but that just seems ugly… It’s only possible to send simple variables (not objects) via the intent? There must be a better way to do this. I have been surfing the web for hours. Any help would be greatly appreciated.
The example found on Android Developers [http://developer.android.com/reference/android/app/Service.html#stopSelf(int)] was a great help to implement a local binder.
For those of you out there interested here is some more information on my implementation so far.
- Once the onServiceConnected function is called a function in the service is called to set which activity it is bound to. After that both service and activity can interact with each other back and forth.
- Make sure that the thread in the service class has a handler. This enables the thread to send a message to its handler once it has completed its task. The handler then sends the result from the service back to its bound activity. Doing this through a handler makes sure the application is back on the main thread when this is done.
This excludes using broadcast and the onBind() function if I understand things correctly?
Data exchanged between client and service via a local
Binder are local method calls within a JVM.
If you are supporting API Level 4 and higher, you can also use
setPackage() on an
Intent to securely send a broadcast to a particular package.
How do I go about doing this?
Put extras on the
Intent you use with
bindService(). Or call a method on the local