当onTaskRemoved是calles时,如何防止服务停止?

时间:2022-01-24 02:54:19

I have a problem with music Service being stopped after "Swipe to exit" being performed on recent apps list in ICS. Service was started with START_REDELIVER_INTENT and it is automatically restarted after few seconds, but I want to prevent the stop at all.

在ICS中的最近应用列表中执行“滑动退出”后,我的音乐服务停止了问题。使用START_REDELIVER_INTENT启动服务,几秒后它会自动重启,但我想阻止停止。

"Swipe to exit" behaviour is generally unknown and I base my whole knowledge on this thread. It contains comments from one of Google employees:

“滑动退出”行为通常是未知的,我将我的全部知识基于此线程。它包含一位Google员工的评论:

[W]hat specifically happens when you swipe away a recent task is it: (1) kills any background or empty processes of the application (see http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html#Lifecycle for what this means), and (2) uses the new http://developer.android.com/reference/android/app/Service.html#onTaskRemoved(android.content.Intent) API to tell any services of the application about the task being removed so it can do whatever it thinks is appropriate.

当您向上移除最近的任务时,特别会发生这样的情况:(1)杀死应用程序的任何背景或空白进程(请参阅http://developer.android.com/guide/topics/fundamentals/processes-and- threads.html#Lifecycle for this what that),(2)使用新的http://developer.android.com/reference/android/app/Service.html#onTaskRemoved(android.content.Intent)API告诉任何关于正在删除的任务的应用程序服务,以便它可以做任何它认为合适的事情。

and

Actually, removing an entry in recent tasks will kill any background processes that exist for the process. It won't directly causes services to stop, however there is an API for them to find out the task was removed to decide if they want this to mean they should stop. This is so that removing say the recent task of an e-mail app won't cause it to stop checking for e-mail. If you really want to completely stop an app, you can long press on recent tasks to go to app info, and hit force stop there. For stop is a complete kill of the app -- all processes are killed, all services stopped, all notifications removed, all alarms removed, etc. The app is not allowed to launch again until explicitly requested.

实际上,删除最近任务中的条目将终止该进程存在的任何后台进程。它不会直接导致服务停止,但是有一个API可以让他们发现任务被删除以决定他们是否希望这意味着他们应该停止。这样,删除说电子邮件应用程序的最近任务不会导致它停止检查电子邮件。如果你真的想完全停止一个应用程序,你可以长按最近的任务来转到应用程序信息,然后点击力量停在那里。停止是完全杀死应用程序 - 所有进程都被终止,所有服务都被停止,所有通知被删除,所有警报都被删除等。在明确请求之前,不允许应用再次启动。

According to second comment, my Service should be able to decide for itself what it wants to do - instead it is stopped without any action from me and gets restarted later.

根据第二条评论,我的服务应该能够自己决定它想要做什么 - 而不是在没有任何操作的情况下停止并稍后重新启动。

Any ideas what to do with it? Using START_STICKY works exactly the same...

任何想法如何处理它?使用START_STICKY完全相同......

1 个解决方案

#1


0  

Put your Service in foreground. Note that you will have to display an ongoing Notification while the Service is running in foreground.

将您的服务放在前台。请注意,当服务在前台运行时,您必须显示正在进行的通知。

#1


0  

Put your Service in foreground. Note that you will have to display an ongoing Notification while the Service is running in foreground.

将您的服务放在前台。请注意,当服务在前台运行时,您必须显示正在进行的通知。