1

I'm making an Android Library Project for playing music files through our API. At the moment I'm using a MusicService (with a media player) to play music files. The MusicService contains an instance of a radio. A radio is providing the MusicService with music files. So a radio can be for retrieving files on the SD card, of retrieving files through an api call, ... The type of radio can be different along different apps built with the same library.

Now, I was wondering... If I create 2 applications with this same library. One application uses the radio with the SD card, the other one the radio with songs from the api. Will these 2 applications interfere with each other, both manipulating the same service?

So when I first start playing from the SD card app, hitting play, the service starts playing. If then, I open the second app on the device and hit Pause, will it pause the music that was playing from the other app?

I know it sounds complicated, but I hope I made my question clear.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
hans_dc
  • 529
  • 1
  • 5
  • 15
  • Your Service lives in the library project? Then each application that uses this lib project will start its own instance of the Service, and they won't conflict. – IgorGanapolsky Feb 27 '15 at 21:11

2 Answers2

3

Android library projects are merged into the APK of each app that uses them (at build time). Once they are merged in they are completely separate, separate process, etc (by default). So, you should not have any overlap or interference between different apps on the same device using the same library.

http://developer.android.com/tools/projects/index.html#LibraryProjects

Here's a related question with a much more in-depth answer: Android Shared Service?

Community
  • 1
  • 1
Charlie Collins
  • 8,806
  • 4
  • 32
  • 41
-1

Yes, the service will interfere, unless, you launch them with intents that are not identical. As per my experience, only one of them will be triggered, and this would probably show you erratic behavior.

What you could do it, perhaps, define separate actions for your service in both the applications, and use implicit intents to launch them.

If you are using explicit intents, I think it might work, but not sure. Have you observed anything yet?

Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
  • "Yes, the service will interfere" I don't think that's true. They will use a separate copy of the same service, not the same service (by default anyway, unless you set the process ID and do some other plumbing). – Charlie Collins Jan 22 '13 at 17:53
  • On second thoughts, perhaps you are correct. But I am not sure when you use implicit intent to launch the service. – Kumar Bibek Jan 22 '13 at 17:56
  • If you're exporting the intent so that you want external applications to use it (implicit), and you do that in BOTH applications (both apps say they service the same intent), then yes, you could cause a conflict, but not at the library project level, at the app that uses the library project level (via the settings in the manifest for how you want to treat the service and what intents you support). – Charlie Collins Jan 22 '13 at 17:59
  • Separate apps run wth separate Linux pid's, period! Therefore, they cannot launch two instances of the same Service. Especially given the fact that each app will use a separate copy of the library project, as @hans_dc originally described in this post. – IgorGanapolsky Feb 27 '15 at 21:15