服务:如果开始bindService onTaskRemoved不叫不叫、bindService、onTaskRemoved

2023-09-07 17:01:36 作者:活在自由地

一个服务已onTaskRemoved()方法来实现。结果当服务开始与startService()onTaskRemoved()调用的时候,应用程序通过刷卡近期-应用-列表中删除。结果但是,如果该服务开始bindService(),然后onTaskRemoved()从来没有所谓的。搜索结果如何使服务调用onTaskRemoved()时,应用程序通过刷卡最近-应用程序列表中删除它已经开始与bindService()?

以下方法调用,如果开始的:

  1。 bindService():活动:的onCreate()CustomService:的onCreate()CustomService:调用onStart()CustomService:onStartCommand()CustomService:onBind()//这里我们刷卡,从应用程序中删除最近-应用程序列表CustomService:onTrimMemory()2. startService():ActivityA:的onCreate()CustomService:的onCreate()CustomService:调用onStart()CustomService:onStartCommand()CustomService:onBind()//刷卡从最近-应用程序列表中删除应用程序CustomService:onTrimMemory()CustomService:onTaskRemoved()//< =====CustomService:onUnbind() 
课程活动大变脸 管理服务全升级

解决方案

一个服务可以绑定或启动或两者兼而有之。这取决于你如何实现 onStartCommand onBind 说的文档。

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

但是,如果你希望你的服务做的都停留在和超过一个IBinder与客户交谈,然后只需启动该服务,然后绑定到它。

  startService(新意图(背景下,CustomerService.class));//绑定到服务bindService(新意图(背景下,CustomerService.class)                        mConnection,Context.BIND_AUTO_CREATE); 

A Service has onTaskRemoved() method implemented. When the service started with startService() onTaskRemoved() called when the app removed from recent-apps-list by swipe. But if the service started with bindService(), then onTaskRemoved() never called. How to make Service call onTaskRemoved() when app removed from recent-apps-list by swipe after it has been started with bindService()?

Following methods called 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()

解决方案

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

 
精彩推荐
图片推荐