android - 当我不需要进程间通信时,Android Service 有用吗?

标签 android service

似乎当我不需要进程间通信时,几乎没有理由使用服务。我知道的唯一原因是:如果我的进程有一个已启动的服务,则该进程被杀死的可能性较小。

我可以只拥有一个带有 dontWantToBeKilled() 和 canBeKilled() 方法的实用程序类,这将启动/停止虚拟服务。除此之外,我不会使用服务。这样对吗?

最佳答案

是的,还有其他原因。

您的应用程序运行在一个进程中,该进程在需要更多资源时可以被系统终止。

根据 this正在运行的服务比不在前台的 Activity 具有更高的优先级,这意味着系统更有可能终止在后台具有 Activity 的应用程序进程,而不是在后台运行服务的应用程序进程。

服务文档指出:

If the service has been started, then its hosting process is considered to be less important than any processes that are currently visible to the user on-screen, but more important than any process not visible. Because only a few processes are generally visible to the user, this means that the service should not be killed except in extreme low memory conditions.

因此,您可以使用服务来降低应用程序进程被终止的可能性。

关于android - 当我不需要进程间通信时,Android Service 有用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8310364/

相关文章:

java - ListView 更改文本中的文本颜色

android - 使用 Android Proguard 编译错误 : can't find references?

android - 应用程序未启动 - React Native (Android)

android - Espresso 。执行错误 'load adapter data'

java - 使用 putExtra 将上下文传递给服务

linux - 制作 Linux Web 服务的最佳方式?

android - 在 android 的嵌套 ExpandableListView 中没有子项可见

java - 如何阻止应用程序/服务被破坏?

android - 为什么Android服务在测试时不是单例的?

linux - Elasticsearch 服务在几个小时后停止