android - 调用 onTaskRemoved 时如何防止服务停止?

标签 android service android-service

在 ICS 中对最近的应用程序列表执行“滑动退出”后,音乐 Service 停止了,我遇到了一个问题。服务以 START_REDELIVER_INTENT 启动,几秒钟后自动重启,但我想完全阻止停止。

“滑动退出”行为通常是未知的,我的全部知识都是基于这个 thread . 其中包含一位 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.

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.

根据第二条评论,我的 Service 应该能够自行决定它想做什么——而不是它在我没有采取任何行动的情况下停止并稍后重新启动。

知道如何处理它吗?使用 START_STICKY 的效果完全一样......

最佳答案

将您的服务放入 foreground .请注意,当服务在前台运行时,您必须显示持续通知。

关于android - 调用 onTaskRemoved 时如何防止服务停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16483695/

相关文章:

android - 如何将 ".apk"下载为 ".apk"? (不是 ".zip")

python - 用 python 编写的 Windows 服务,未检测到关闭事件并正常停止

java - Android 服务持久化

Android 服务未通过警报启动

android - 后台服务提供数据供以后使用最佳实践

java - Watson视觉识别,在java中针对多个分类器进行分类

android - 未经联系人许可,GoogleAccountCredential.setSelectedAccountName(String) 无法在 Android 6.0 上运行

android - 启动服务时出现 NullPointerException

android - 错误 : getSystemServise unresolved reference - kotlin android studio

android - 无法为拖放示例解析导入 android.*