18

I have a Service in which the onTaskRemoved() method has been implemented.
When the service is started with startService() the function onTaskRemoved() is called when the app is removed from the recent-apps-list by swipe.

But if the service is started with bindService(), then onTaskRemoved() never called.

How can I make the Service call onTaskRemoved() when app removed from recent-apps-list by swipe after it has been started with bindService()?

Android calls the following lifecycle methods if started with:

1. bindService():
Activity﹕ onCreate()
CustomService﹕ onCreate()
CustomService﹕ onStart()
CustomService﹕ onStartCommand()
CustomService﹕ onBind()
// Here we swipe to remove app from recent-apps-list
CustomService﹕ onTrimMemory()

2. startService():
ActivityA﹕ onCreate()
CustomService﹕ onCreate()
CustomService﹕ onStart()
CustomService﹕ onStartCommand()
CustomService﹕ onBind()
// Swipe to remove app from recent-apps-list
CustomService﹕ onTrimMemory()
CustomService﹕ onTaskRemoved() // <===== 
CustomService﹕ onUnbind()
James Jordan Taylor
  • 1,560
  • 3
  • 24
  • 38
surlac
  • 2,961
  • 2
  • 22
  • 31
  • 4
    Important note: `onTrimMemory()` is called everytime the app goes in background, you do not have to "swipe to remove" app in order to trigger it. This will occurs if you press the Home Button. So your comment is misleading. – ForceMagic Jan 08 '15 at 21:15
  • Try this hack (working) - https://stackoverflow.com/a/49489478/3879847 – Ranjithkumar Mar 26 '18 at 10:47

1 Answers1

15

A service can be bound or started or both. It depends on how you implement onStartCommand and onBind say the docs.

http://developer.android.com/guide/components/services.html

But if you want your service to do both stay around and talk with clients over an IBinder then simply start the service and then bind to it.

startService(new Intent(context, CustomerService.class));
// Bind to the service
bindService(new Intent(context, CustomerService.class),
                        mConnection, Context.BIND_AUTO_CREATE);

Edit 20220314. "future versions of the os" may end the service when it doesn't have a notification card for the user to interact with the service.

... lots have changed since I answered this I got a recent vote so here I am. You want your service to run in the background have a notification going and a way for the user to interact with the service in the notification. You've seen them the play pause controls on music and video notifications. Without a notification the os might end your service.

danny117
  • 5,581
  • 1
  • 26
  • 35
  • I am doing the same and onStartCommand() also called, but service is not running in background. Application destroy then service also destroy .. can you help? – Nooruddin Lakhani Aug 26 '17 at 05:21
  • I'd need to see a bit more of your code to help. Ask a question post some code. Someone here at SO will help. – danny117 Aug 28 '17 at 17:27
  • Its working a bit, when app destroy, service actually stops and restart again. Its onCreate() and onStartCommand() again call, I just want the service to not to stop and restart again. I just want it to run continuously in background. Any idea how to achieve this ? – Nooruddin Lakhani Aug 29 '17 at 06:09
  • Lots changed since I answered this. – danny117 Mar 16 '22 at 02:40