Android的共享服务? [英] Android Shared Service?

查看:118
本文介绍了Android的共享服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想说两件事。第一个是,对不起,如果这个问题已经被问,我已经搜索周边的这个主题类似的问题,但无法找到一个解决方案。其次遗憾的冗长的问题,请让我知道任何错误,我一定会做出相应的改变:)

我是比较新的Andr​​oid开发(约2个月),所以请原谅我的无知。我的问题是关于Android的服务。

我的问题如下,我创建了以下3个应用程序:

  • 在一个Android库,它包含一个小的测试服务(为myService )。
  • 在应用程序( TestApplicationOne ),它可以访问Android的库。
  • 在另一个应用程序( TestApplicationTwo ),这也可以访问Android的库。

我目前的解决方案的工作原理如下, TestApplicationOne 引用自定义库,并使用这个库连接到服务(为myService )通过的 bindService()的方法。一旦连接成功的应用程序,然后将自身添加到位于为myService内观察家的集合。在此集合中的每个对象被通知每一次服务需要广播消息。

在跑,上面的解决方案似乎正常工作。不过,我现在有另一个应用程序( TestApplicationTwo ),这还要使用相同的服务,如上图所示。实施 TestApplicationTwo 被创造的第一个应用程序相同的工作原理/规格( TestApplicationOne )。

这个问题我有是,当该服务已启动在任一应用程序,其他应用程序没有任何事件的通知。

我试图实现几个方法来解决这个问题。如使用Singleton模式保留一个实例,但问题似乎仍然存在。这个我唯一的COM prehension的是,无论是应用程序每次启动时,在创建库的一个新的实例。因此,库中引用的 TestApplicationOne 是不一样的实例作为参考库 TestApplicationTwo ,然后作为一个结果,而不是被通知。

有没有人在这个问题上的经验吗?或者能想到的任何可能的解决方案?

感谢您事先的任何帮助,这是很AP preciated。

约翰·

解决方案
  

这个问题我有是,当该服务已启动在任一应用程序,其他应用程序没有任何事件的通知。

那是因为你有两个服务。仅仅因为该服务被打包在一个Android库项目不会神奇地使两个不同的应用程序使用同一个副本。

  

如使用Singleton模式保留一个实例,但问题似乎仍然存在。

这两个应用程序运行在单独的进程,以服务自己的私人副本。

  

这个我唯一的COM prehension是,每次启动时或者应用程序,该库的一个新的实例被创建。

图书馆没有实例。 Android的库项目都没有DLL的 - 他们更类似于静态库

  

因此​​,在TestApplicationOne引用的库是不一样的实例作为TestApplicationTwo引用的库,并且作为结果,没有被通知

更准确地说,在 TestApplicationOne 的服务是从复制服务的单独副本 TestApplicationTwo

  

或者能想到的任何可能的解决方案?

不要有两个单独的应用程序。

或者,重新设计的应用程序,使得只有一种具有该服务,而另一个使用来自所述第一应用程序的服务。

或者,已禁用的体现在这两个应用程序的服务,出口有一个稳定的<意向滤光器> (例如,用一个自定义操作字符串)。第一个应用程序被安装/运行使用 PackageManager ,看是否服务存在(例如,它不是真正的第一个应用程序) - 看到该服务不存在,它使自己的该服务的副本。然后,第二个应用程序的经过同样的过程,认为该服务已经存在,并使用远程副本,而不是使自身的

无论是第二或第三是很容易的,仅仅是因为数据只存在于一个应用程序(一个与服务)。因此,如果该应用程序被卸载,其他的应用拧,即使它恰好有服务code可作为禁用的组件。如果应用程序有明确的说明,以用户为具有主/辅关系(例如,应用程序和插件),那么这可能会制定出 - 用户希望不会对一个插件来工作,如果应用程序是不存在的,例如。当然,在这种情况下,插件不会有其自己的在首位的服务的副本,但总是着眼于主要的应用程序的那个

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 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.

这篇关于Android的共享服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆