Android 服务:进程与非进程

发布于 2024-09-30 01:29:23 字数 54 浏览 2 评论 0原文

将服务放在单独的进程中或将其保留在应用程序的主进程中之间有什么实际区别?每个场景的用途是什么?

What are the practical differences between putting a service in a separate process or keeping it in the app's main process? What would each scenario be used for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

久伴你 2024-10-07 01:29:23

当服务在主进程中运行时,如果您的应用程序因某种原因崩溃,它将停止。对于某些可以从不同应用程序使用的服务或应独立于主应用程序运行的服务,将服务放入其自己的进程中是合理的。

When a service is running in the main process it will be stopped in case your application crashes for whatever reason. Put a service into it's own process is reasonable for some services which can be used from different applications or services which should run independently from your main app.

动次打次papapa 2024-10-07 01:29:23

我认为将服务放入另一个进程中的唯一原因是

  1. 应用程序占用大量资源,并且可能会很快被操作系统杀死。将服务放在单独的进程中将分配资源,如果您的应用程序终止,您的服务也不会。
  2. 万一您的应用程序出现错误并终止,您的服务将继续运行。

但是,如果您创建一个好的应用程序并使用好的编程,您就不应该遇到这些问题。通过将您的服务放在单独的进程中,它会导致 SharedPreferences 和并发数据库访问等问题......我建议不要这样做。

更不用说...另一个进程意味着另一个DVM。与在一个 DVM 中运行相比,这会占用更多资源,并且速度会变慢。

The only reasons I see for putting a service in another process is

  1. The application is resource heavy and likely going to be killed quickly by the OS. Putting the service in a separate process will distribute the resources and if you application dies your service won't.
  2. JUST in case your application has errors and dies your service will keep going.

However if you create a good application and use good programming you should not run into either of these issues. By having your service in a separate process it causes trouble with things like SharedPreferences and concurrent DB access... I would recommend not doing it.

Not to mention... another process means another DVM. This will take more resources than running in one DVM and slow things down.

心的憧憬 2024-10-07 01:29:23

此外,将服务放在另一个进程中会使您对静态变量的更改对于主进程不可见。当你为一个变量分配一些值时,你可以得到这样的情况,并且它没有改变!为了这个问题我花了一整天的时间!

Also putting service in another process makes your changes of static variables invisible for main process. You can get situation, when you assign a variable with some value, and it is not changed!! I spent whole day for this issue!

相权↑美人 2024-10-07 01:29:23

以下引用自 Android 开发人员网站

请注意,服务与其他应用程序对象一样,在其托管进程的主线程中运行。这意味着,如果您的服务要执行任何 CPU 密集型(例如 MP3 播放)或阻塞(例如网络)操作,它应该生成自己的线程来完成该工作。

Jake 指出,您可以通过清单控制正在运行的进程的名称。但以下来自文档的发现:

关于 Service 类的大多数困惑实际上都围绕着它不是什么:

  • 服务不是一个单独的进程。 Service 对象本身并不意味着它运行在自己的进程中;除非另有说明,否则它与其所属的应用程序在同一进程中运行。

这很有趣,这里所说的是:

运行服务的进程的名称。通常,应用程序的所有组件都在为应用程序创建的默认进程中运行。它与应用程序包具有相同的名称。元素的 process 属性可以为所有组件设置不同的默认值。但组件可以使用自己的进程属性覆盖默认值,从而允许您将应用程序分布在多个进程中。

但无论如何,如果您需要将 Service 暴露给其他应用程序,例如,您需要向其他应用程序提供内容(如电话簿),则将 Service 设置为在不同进程中运行就是原因。

Following is a quote from Android Developer's web site.

Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work.

Jake points out that you can, thru manifest, control the Name of the process it is running. But following findings from Documentatioin:

Most confusion about the Service class actually revolves around what it is not:

  • A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.

This is interesting, what is said Here is:

The name of the process where the service is to run. Normally, all components of an application run in the default process created for the application. It has the same name as the application package. The element's process attribute can set a different default for all components. But component can override the default with its own process attribute, allowing you to spread your application across multiple processes.

But anyway, if you need Service to be exposed to other applications, for example, you need to provide content (like phonebook) to other applications, setting service to run in different process is the reason.

信愁 2024-10-07 01:29:23

使用服务的进程属性会被清单解析器拒绝,因此这是相当误导的!

Using the process attribute of a service is rejected by the manifest parser so it is rather misleading!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文