我想知道为什么即使我已经离开(按下后退按钮)上面图片中的Flickr和Messenger应用程序仍然有一个进程在运行?
最近我写了一个包含服务的应用程序,我发现如果仍然有一个进程正在运行,该服务将不太可能被系统杀死。
那么上面的应用程序如何让进程在后台运行?
答案 0 :(得分:0)
他们使用后台服务,即使你退出应用程序也会保持活力......正如文档所说
The Android system will attempt to keep the process hosting a service around as long as the service has been started or has clients bound to it
只要应用程序未结束,除非没有内存,否则服务将保持活动状态。
When running low on memory and needing to kill existing processes, the priority of a process hosting the service will be the higher of the following possibilities:
If the service is currently executing code in its onCreate(), onStartCommand(), or onDestroy() methods, then the hosting process will be a foreground process to ensure this code can execute without being killed.
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.
If there are clients bound to the service, then the service's hosting process is never less important than the most important client. That is, if one of its clients is visible to the user, then the service itself is considered to be visible.
A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)
您可以详细了解服务here
答案 1 :(得分:0)
后退按钮不会结束应用程序,就像在Windows中最小化它们一样。要结束他们,他们需要打电话完成。 Androiud的设计(我认为这是一个愚蠢的,不安全的想法,但它的工作原理)是应用程序不会退出,除非他们退出自己的完成或你内存不足并且操作系统杀死它们。