My application is searching for data, given various filtering parameters. This data is acquired from multiple sources (i.e. multiple sites over Internet). I want other developers to be able to implement their own data sources, so they could “plug” them into my application without any action on my part.
What is the best way to achieve that? In particular:
- What should the external developers do to make their implementations visible to my process?
- How do I discover such external implementations?
- Once discovered, what can I use as an identifier for each of these implementations?
- How to communicate with various “methods” of the external implementation.
My best guess is to use services:
- Have external developers set up their service with intent-filter with a specified URI.
- Use PackageManager#queryIntentServices method to list all services matching a specified URI.
- Use their package name as identifiers, so I can fire up intents directly at that package.
- Believe that external developers listen and properly respond to all intents specified in my docs. I will pass them either PendingIntent or Messenger objects to get the results back.
I haven’t yet tried it and I am a beginner in Android. Is this the proper way?
If by "URI" you mean "action", this is certainly one approach.
Yes.
Those would work. I’d lean towards PendingIntent.
:: shrug ::
"Proper", like truth and beauty, lies in the eye of the beholder.
(and hopefully not that beholder — those things were nasty)
There is nothing intrinsically wrong with your approach. I might do the same thing, modulo my comments above.
Some alternatives:
Same strategy, but have the service export a binding interface with AIDL, with your host binding to them (advantage: richer API, disadvantage: bigger pain to set up, particularly if your host is an activity)
Plugins are
ContentProviders, which you discover by an authority naming convention or some bootstrap methodPlugins really communicate with you via broadcast
Intents(works well if the predominant data flow is them pushing data to you rather than you pulling data from them)