开发者

Sharing resources between service and application in android

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.开发者_StackOverflow


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜