androidandroid-service

If the app offering the bound service is not running, can external components still use it? Does the app need to be running?


I've read that maybe I should declare <service android:enabled="true" ... /> or android:exported="true" in Manifest.xml, or I should use pass BIND_AUTO_CREATE to bindService().

Since I don't have experience in developing Android apps, I'm just curious.

For example:

MyApp has a bound service called "MyService" which is public. MyApp is NOT running.

MySecondApp is running and tries to bind "MyService".

What happens? Is it possible? How?


Solution

  • If a Service declared in your app's manifest is running then your app is running. One of the purposes of Services is to be alive independent of any Activity that can be visible on the screen.

    Your service is "bound" when you use e.g. a ServiceConnection to bind to it and the onBind() method inside the Service is called.

    Otherwise all you have is a Service declared in the Manifest that is exported. It is not running (unless you start it) or bound (unless you bind it).

    I researched your question a little and it seems you want to control service1 defined in a Manifest in App1 from App2. This is a duplicate of: How to start android service from another Android app

    The short answer is yes, you can do this:

    1. Declare your Service in App1 in the Manifest with exported="true",

    2. In App2 interact with your service using an Intent and specify the full package name for App1 in the Component,

    3. Your Service probably must be started as a foreground service using startForeground() and you must provide a Notification. This is because probably there is no Activity from App1 on the screen.