1

I have an Android Library with a service, which I implemented using AIDL. I want a single instance, cross application to be used with other apps. So I have a base app with the service and I managed to make the library for other apps to use the same instance.

My problem comes when I close all apps using the service, because for every app, the on destroy unbinds from the service, but the service is still running.

Also, I'm only using the service by binding, not by startService().

I checked through android studio that the service is running after closing the apps, and the counter I have on the service for each bind/unbind call is 0!. I increment the counter when there is a call to bind, and decrease for calls to unbind. My only way to make the service stop is by opening the base app, which has the service defined and closing it.

Edit: Also noticed that onBind is being called only once, even for other apps that are binding, but the reference for all those apps is still the same service, they share the same information and only one Service is shown in the android studio.

Edit: I've also observed that if one of the apps using the service is the base app, if I close it, the service dies and another one is started, the other apps don't notice the change, they keep using the service as if nothing happened, which is understandable because it's a remote service.

What is happening and what can I do about it?

  • "but the service is still running" -- what is your definition of "still running"? How are you determining that it is still running? – CommonsWare May 08 '18 at 17:18
  • I can still see it running on the android studio, over the logcat. Also, I can see it's debug prints – Bruno Alberti May 08 '18 at 17:38
  • My guess is that you forked some threads and did not clean them up (e.g., in `onDestroy()`). Those threads will continue to run until the process is terminated. – CommonsWare May 08 '18 at 17:42
  • They would be listed on android studio, on the android device manager wouldn't they? I tried to make only 1 app use the service (which is not the base app), I did override onDestroy and called unbind in it, even so, the service still lingers. – Bruno Alberti May 08 '18 at 18:12
  • "They would be listed on android studio, on the android device manager wouldn't they?" -- I do not know what "they" refers to. "I did override onDestroy and called unbind in it, even so, the service still lingers" -- I was referring to the service's threads and the service's `onDestroy()`. – CommonsWare May 08 '18 at 18:13
  • "they" was a reference to the threads you mentioned, the only they that could be referenced in your comment. I will check if it could be from some thread in the service, as well as something on onDestroy(). Thanks for the quick help. – Bruno Alberti May 08 '18 at 18:19
  • My service has no threads that are being forked with the app I tested, and I have no overriden onDestroy. What bothers me the most is that onBind is only called once, even with more apps binding to the device. – Bruno Alberti May 08 '18 at 19:01
  • Oh, sorry, I found that onBind is only called once, the same reference is passed to other calls – Bruno Alberti May 08 '18 at 19:05
  • You said that your service is still logging messages to LogCat. This means that you still have code executing on some threads. Otherwise, you cannot have log messages coming from your app. – CommonsWare May 08 '18 at 19:14
  • I tried to reproduce this behavior with a minimum service and app and managed to observe the same thing. I made a service int a library with a simple aidl, and two apps that use them. The first app sends an intent with a component that has as the package, it's own package, and as class the String for the full class name defined in the library. The second app does the same with the intent, except for the package name, which instead of using it's own package, it uses the package of the first app. This way, both apps use the same service, but when the first app is closed, the service dies. – Bruno Alberti May 08 '18 at 20:17
  • Adding to that, if I only start the second app, upon closing it, the service is still running, I have not set LogCat prints on this one, but I can see it running on Android Device Monitor. Is my way of binding to the service with the intent made this way the issue? – Bruno Alberti May 08 '18 at 20:18
  • What does "but I can see it running on Android Device Monitor" mean? Do you mean that you are seeing the *process* running? If so, that is to be expected. Android will terminate the process when it needs to, in order to reclaim system memory for use by other processes. – CommonsWare May 08 '18 at 20:24
  • I meant that the process which I specified for the service is listed on Android Device Monitor. I expected the service to be destroyed when there was no other application bound to it. Why is that not the case? – Bruno Alberti May 08 '18 at 20:30

1 Answers1

0

For the two points being questioned:

I was able to verify what CommonsWare said that the process in which the service is run is still up, but the service itself is not. At first I had a thread running in the service after every app unbound, but after making sure it wasn't up in the end made the service be destroyed.

The second issue, regarding the service being destroyed while still bound with other activities was solved by seeing this link which describes a bug in android that kills services when it shouldn't. My case was simply solved by making my service run in the foreground, which I didn't know was possible.

After some tests I verified the service is still intact as long as any app is bound with it, and that the service indeed is destroyed correctly after no more app is bound.