tags:

views:

169

answers:

3

Hi All,

The code I am trying to implement includes a service and an application. When the application is first launched, it starts the service using the startService(svc_name) call. Here svc name is an intent pointing to the class that runs the service.

I want to share a resource(file/socket connection) between the service and the application. For example one writes to a file and another reads from it. I am unable to get proper sync between the service and app.

Could you please let me know how this can be achieved? Thanks in advance.

A: 

proper way of communicating with a service is thru RPC. android RPC library is very easy to work with, just look for Remoting examples in ApiDemos.

Reflog
That is only proper if the `Activity` and `Service` are in separate applications. It is unclear from the question whether they are in this person's case or not.
CommonsWare
+1  A: 

If your app and service are two independent applications (different .apk files) you can look into using aidl. The aidl allows you to send and receive messages across processes.

But, if your app(Activities) and service share the same process (same .apk file) then you can use SharedPreference, or static members. The SharedPreference is accessible by all threads of your application, and is persistent between runs.

http://developer.android.com/reference/android/content/SharedPreferences.html

It can only handle simple types like Boolean, Integer, Long, Float, and String though.

Jason D.
A: 

Hi All,

Thanks for your replies. The application I am talking about is the "activity". And yes both the activity and service are in the same application.

Thanks for the posible solutions. I will try them out and post the result.

ani123