开发者

How does intents handle multiple services or classes of the same type?

开发者 https://www.devze.com 2023-03-03 01:30 出处:网络
Say I have an application, where开发者_如何学运维 there is a service running in a loop, this service is started by an intent. I then have another application (the same, but with a different AppID) wan

Say I have an application, where开发者_如何学运维 there is a service running in a loop, this service is started by an intent. I then have another application (the same, but with a different AppID) wanting to start the same service, while the prime service is still running. How can intents handle this? (Note: I want both services to run at the same time...)

I'm looking for an understandable answer that explains the "HOW" behind it, not as much: "well this code takes care of THAT!". It's no problem if your answer doesn't fit my example, as I'm still VERY new my question might be asked in an odd (or even dumb) way, I apologize beforehand.

Thanks! - Bobby


A Service has to be declared in the AndroidManifest.xml: this specifies the Service class. You can launch the Service using either an explicit Intent (specifying the service class (and optionally the component name)) or using an Intent that will match an IntentFilter associated with the service.

Case 1 is using an explicit Intent. If only 1 app registers the class as a Service then in your second app you will have to use an Intent with a ComponentName that wll start the first app's copy of the service. In this case, only 1 instance of that Service class will be active. If both apps register the class as a Service your intent will launch a second instance of that Service class.

Case 2 is using an IntentFilter. If both Service declarations use the same IntentFilter then IntentFilter priorities kick in: the Service declaration with a higher priority fires off, and will essentially always win, so you will only ever have 1 instance of that Service class running. If both Service declarations have the same priority then its a tossup which one gets used.

In any case there will be (for any 1 Service declaration) at most one copy of that service running at any time. You can get crazy with "remote" process declarations but that's about it I think.

0

精彩评论

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

关注公众号