2

In Android Oreo why bound services still allowed whereas (Started Services & IntentServices) are not allowed.

Suppose I have a bound service which I am binding in my oncreate() method of my activity and unbinding it in my ondestroy() method of my activity. Now when a user comes to this activity and presses the home button because of which my app goes in background now for the indefinite amount of time this bound service will use resources which I think developers wanted to remove from Oreo, So don't you think what was the reason behind keeping the bound services if they can also waste memory resources.

Sudhanshu Gaur
  • 7,486
  • 9
  • 47
  • 94

1 Answers1

3

In Android Oreo why bound services still allowed whereas (Started Services & IntentServices) are not allowed.

All of those services are allowed. However, a started non-foreground service can only run for ~1 minute.

now for the indefinite amount of time this bound service will use resources

Android will terminate your background process after a while, no different than before. A purely bound service does not raise the process importance; only a started service does.

However, a bound service's process' importance is governed both by the service's own process and the process of any bound client. That's why bound services are not directly affected by the Android 8.0 changes — the life of the service is governed more by the client than it is by the service itself. So, in cases where the service is bound to by a core OS process (e.g., NotificationListenerService), the system effectively controls how long that service needs to be around. In your case, since the client and the service are in the same app and the same process, your process can be terminated in the background normally.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • From where in the docs time is mentioned which is 1 minutes `a started non-foreground service can only run for ~1 minute` ?? Does it mean if my service will finish its work in less than a minute then I can use it without any problem? – Sudhanshu Gaur Sep 16 '17 at 17:36
  • 1
    @SudhanshuGaur: The [docs](https://developer.android.com/about/versions/oreo/background.html#services) say "several minutes". In testing, it appears to be about one minute. – CommonsWare Sep 16 '17 at 17:37
  • So for which scenario will bound services not terminate normally then ?? – Sudhanshu Gaur Sep 16 '17 at 17:53
  • @SudhanshuGaur: Sorry, but I do not understand your question. – CommonsWare Sep 16 '17 at 17:56
  • Sorry but as you wrote here `In your case, since the client and the service are in the same app and the same process, your process can be terminated in the background normally.` that's why I wanted to ask as system will terminate my bound service then can you tell me some example where system will not terminate bound service (keep it running in the background) ?? – Sudhanshu Gaur Sep 16 '17 at 17:57
  • 1
    @SudhanshuGaur: [The documentation that I linked to](https://developer.android.com/about/versions/oreo/background.html#services) has: "An app is considered to be in the foreground if... another foreground app is connected to the app, either by binding to one of its services or by making use of one of its content providers. For example, the app is in the foreground if another app binds to its: IME, Wallpaper service, Notification listener, Voice or text service". Your service has foreground importance if a bound client has foreground importance. – CommonsWare Sep 16 '17 at 17:59
  • Can you please see my question https://stackoverflow.com/questions/53794742/checking-if-any-new-message-has-arrived-or-not-without-asking-for-any-permission?noredirect=1#comment94444225_53794742 It would be really helpful for me. Thanks. – Sudhanshu Gaur Dec 16 '18 at 14:47