服务:如果使用bindService启动,则不会调用onTaskRemoved

sur*_*lac 17 android android-service android-service-binding

服务已实现onTaskRemoved()方法.
当通过swipe从recent-apps-list中删除应用程序时调用startService()onTaskRemoved()时启动服务.
但是如果服务以bindService()开头,则onTaskRemoved()从不调用.

如何在使用bindService()启动应用程序后,通过轻扫将应用程序从recent-apps-list中删除,从而调用onTaskRemoved()?

以下方法调用如果以下:

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()
Run Code Online (Sandbox Code Playgroud)

dan*_*117 14

可以绑定或启动服务,也可以两者兼而有之.这取决于您如何实施onStartCommandonBind说出文档.

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

但是,如果您希望您的服务同时保持并通过IBinder与客户交谈,那么只需启动该服务然后绑定到它.

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