开发者

Android Shared Service?

First of all i'd like to say two things. The first being, sorry if this question has already been asked, i've searched for similar questions around this topic but was unable to find a solution. Secondly sorry for the lengthy question, and please let me know of any error and I will be sure to make appropriate changes :).

I am relatively new to Android development (approx 2 months), so please forgive my ignorance. The question I have is regarding the android service.

My issue is as follows, I have created the following 3 applications:

  • An android library which contains a small test service (myService).
  • An application (TestApplicationOne) which has access to the android library.
  • Another application (TestApplicationTwo) which also has access to the android library.

My current solution works as follows, TestApplicationOne references the custom library and uses this library to connect to the service (myService) via the bindService() method. Upon connection successful the application then adds itself to a collection of observers located within myService. Each object in this collection is notified each time the service needs to broadcast a message.

When ran, the above solution seemed to work fine. However, I now have another application (TestApplicationTwo) which would also like to use the same service as the one above. The implementation of TestApplicationTwo was created to the same workings/spec of the first application (TestApplicationOne).

The issue I have is that when the service is started in either application, the other application is not notified of any events.

I have tried to implement several approaches to solve this. Such as using the Singleton pattern to retain开发者_如何学JAVA a single instance, but the problem still seems to exist. My only comprehension of this is that each time either application is started, a new instance of the library is created. Thus the library referenced in TestApplicationOne is not the same instance as the library referenced in TestApplicationTwo, and as a result, not being notified.

Is there anybody with any experience in this issue? Or can think of any possible solution?

Thank you in advance of any help, it is much appreciated.

John


The issue I have is that when the service is started in either application, the other application is not notified of any events.

That's because you have two services. Just because the service is packaged in an Android library project does not somehow magically cause two distinct applications to use the same copy.

Such as using the Singleton pattern to retain a single instance, but the problem still seems to exist.

Both applications are running in separate processes, with their own private copy of the service.

My only comprehension of this is that each time either application is started, a new instance of the library is created.

Libraries don't have instances. Android library projects are not DLLs -- they are more akin to static libraries.

Thus the library referenced in TestApplicationOne is not the same instance as the library referenced in TestApplicationTwo, and as a result, not being notified.

More accurately, the service in TestApplicationOne is a separate copy of the service from the copy in TestApplicationTwo.

Or can think of any possible solution?

Don't have two separate applications.

Or, redesign your apps such that only one has the service, and the other uses the service from the first app.

Or, have the service disabled in the manifest in both apps, exported with a stable <intent-filter> (e.g., with a custom action string). The first app to be installed/run uses PackageManager to see if the service exists (e.g., it's not really the first app) -- seeing that the service does not exist, it enables its own copy of that service. Then, the second app goes through the same process, sees that the service is already there, and uses the remote copy rather than enabling its own.

Neither the second or the third are very easy, simply because the data only exists in one app (the one with the service). Hence, if that app is uninstalled, the other app is screwed, even if it happens to have the service code available as a disabled component. If the apps are clearly described to the user as having a primary/secondary relationship (e.g., app and its plugin), then this may work out -- users hopefully won't expect a plugin to work if the app isn't there, for example. Of course, in this case, the plugin wouldn't have its own copy of the service in the first place, but would always look to the main app for that.


I am not sure, but i think android:singleUser="true" attribute of service tag could be what you are looking for.

If set to true, a single instance of this component will run 
 for all users. [boolean]

If this doesnt help, then you might consider a solution in which you send Broadcasts from the service for each of your events and have BroadcastRecievers in your 2 applications instead of accessing service as an object...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜