安卓共享服务? [英] Android Shared Service?

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

问题描述

首先我想说两件事.首先,很抱歉,如果已经问过这个问题,我已经搜索了有关此主题的类似问题,但无法找到解决方案.其次,对于这个冗长的问题,请让我知道任何错误,我一定会做出适当的更改:)

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

我对 Android 开发比较陌生(大约 2 个月),所以请原谅我的无知.我的问题是关于 android 服务.

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

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

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

  • 一个包含小型测试服务 (myService) 的 android 库.
  • 可以访问 android 库的应用程序 (TestApplicationOne).
  • 另一个也可以访问 android 库的应用程序 (TestApplicationTwo).
  • 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.

我当前的解决方案如下所示,TestApplicationOne 引用自定义库并使用该库通过 bindService()<连接到服务 (myService) 方法.连接成功后,应用程序会将自己添加到位于 myService 内的观察者集合中.每次服务需要广播消息时,都会通知此集合中的每个对象.

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.

运行时,上述解决方案似乎工作正常.但是,我现在有另一个应用程序 (TestApplicationTwo),它也想使用与上述相同的服务.TestApplicationTwo 的实现是根据第一个应用程序 (TestApplicationOne) 的相同工作/规范创建的.

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.

我尝试了几种方法来解决这个问题.比如使用Singleton模式来保留单个实例,但问题似乎仍然存在.我对此的唯一理解是,每次启动任一应用程序时,都会创建该库的一个新实例.因此,TestApplicationOne 中引用的库与 TestApplicationTwo 中引用的库不是同一个实例,因此不会被通知.

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.

约翰

推荐答案

我遇到的问题是,当服务在任一应用程序中启动时,另一个应用程序不会收到任何事件通知.

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

那是因为您有两项服务.仅仅因为该服务被打包在一个 Android 库项目中并不会以某种方式神奇地导致两个不同的应用程序使用相同的副本.

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.

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

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.

库没有实例.Android 库项目不是 DLL -- 它们更类似于静态库.

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

因此,TestApplicationOne 中引用的库与 TestApplicationTwo 中引用的库不是同一个实例,因此未收到通知.

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

更准确地说,TestApplicationOne 中的服务是与 TestApplicationTwo 中的副本不同的服务副本.

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.

或者,在两个应用程序的清单中禁用服务,使用稳定的 (例如,使用自定义操作字符串)导出.要安装/运行的第一个应用程序使用 PackageManager 来查看服务是否存在(例如,它不是真正的第一个应用程序)——看到该服务不存在,它启用自己的副本服务.然后,第二个应用程序执行相同的过程,看到服务已经存在,并使用远程副本而不是启用自己的.

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.

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

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