简体   繁体   English

包含后台服务的Android分发SDK。

[英]Android distributing SDK containing background service.

I'd like to create an android sdk that I can re-distribute. 我想创建一个我可以重新分发的android SDK。 In that SDK, it has a Service. 在该SDK中,它具有服务。

Let's say appA and appB integrates with my SDK, if appA and appB starts the Service, is there going to be 2 Services running or just one? 假设appA和appB与我的SDK集成在一起,如果appA和appB启动服务,那么将运行2个服务还是仅运行一个?

Is there a way to make sure only one Service can be ran? 有没有办法确保只能运行一项服务?

Or is it not a good idea to distribute the service in the SDK? 还是在SDK中分发服务不是一个好主意吗?

In that SDK, it has a Service. 在该SDK中,它具有服务。

I am going to interpret this as meaning "in that SDK, it has a Java class that extends Service , and I am expecting that this Service will be registered in the app's manifest, whether manually or automatically (eg, via manifest merging of an AAR)". 我将其解释为“在该SDK中,它具有一个扩展Service的Java类,并且我希望该Service可以手动或自动(例如,通过AAR的清单合并)注册在应用的清单中)”。

Let's say appA and appB integrates with my SDK, if appA and appB starts the Service, is there going to be 2 Services running or just one? 假设appA和appB与我的SDK集成在一起,如果appA和appB启动服务,那么将运行2个服务还是仅运行一个?

There will be zero, one, or two instances of the service. 该服务将有零个,一个或两个实例。

Zero would be if both developers screwed up and failed to add the service to their manifests, in which case it will never run. 如果两个开发人员都搞砸了,但未能将服务添加到清单中,则该值为零,在这种情况下它将永远无法运行。

Two would be if both developers added the service to their manifests and are starting that service via an explicit Intent . 如果两个开发人员都将服务添加到他们的清单并通过显式的Intent启动该服务,那就是两个。

There are various scenarios in which only one service will run (eg, one developer screws up and doesn't add the service to the manifest, while the other one does and uses an explicit Intent ). 在多种情况下,只有一种服务将运行(例如,一个开发人员搞砸了,没有将服务添加到清单中,而另一种则使用了显式的Intent )。

Is there a way to make sure only one Service can be ran? 有没有办法确保只能运行一项服务?

Not easily. 不容易。 For starters, unless you are the developer of both appA and appB, those developers can do what they want, and they may not want other apps talking to their copy of your service (eg, security reasons). 首先,除非您同时是appA和appB的开发人员,否则这些开发人员可以执行他们想要的操作,并且他们可能不希望其他应用程序与您的服务副本进行交流(例如,出于安全原因)。 You also have to deal with the case where appA is installed but not appB, or appB is installed and not appA. 您还必须处理安装了appA而不安装appB的情况,或者安装了appB而不安装appA的情况。 And you have to deal with the case where first appA and appB are both installed, and they elect to use appA's service, then the user uninstalls appA, -- now appB will not longer have access to the data that service managed on appA's portion of internal storage. 您必须处理以下情况:首先同时安装了appA和appB,他们选择使用appA的服务,然后用户卸载了appA,现在appB将不再有权访问服务在appA的一部分上管理的数据内部存储器。

Or is it not a good idea to distribute the service in the SDK? 还是在SDK中分发服务不是一个好主意吗?

There is nothing intrinsically wrong with distributing a service in a JAR or AAR. 在JAR或AAR中分发服务本质上没有错。 Trying to force multiple apps to use the same service instance, from just one of those apps, is tricky at best and risky at worst, so I would try to avoid that requirement. 试图迫使多个应用程序仅使用其中一个应用程序来使用同一服务实例,这在最好的情况下是棘手的,在最坏的情况下是冒险的,因此,我将尽量避免这种要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM