简体   繁体   中英

Android distributing SDK containing background service.

I'd like to create an android sdk that I can re-distribute. In that SDK, it has a Service.

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?

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?

In that SDK, it has a Service.

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

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?

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 .

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

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). You also have to deal with the case where appA is installed but not appB, or appB is installed and not 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.

Or is it not a good idea to distribute the service in the SDK?

There is nothing intrinsically wrong with distributing a service in a JAR or 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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