Service: onTaskRemoved not called if started with bindService

蹲街弑〆低调 提交于 2019-11-27 23:11:58

问题


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()

回答1:


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);


来源:https://stackoverflow.com/questions/24127923/service-ontaskremoved-not-called-if-started-with-bindservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!