如何创建一个Android的活动和服务使用单独的进程 [英] How to create an Android Activity and Service that use separate processes

查看:1338
本文介绍了如何创建一个Android的活动和服务使用单独的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,包括活动和服务。目前,他们都存在于同一个进程,并使用同一个堆,但我希望有单独的进程/堆的服务。 IE浏览器。我希望该服务是完全独立于活动的,这样,如果该活动崩溃,也不会影响该服务。我这样做,不过,希望他们能够安装一个单一的应用程序。这可能吗?

解决方案

绝对有可能。请参阅的Andr​​oidManifest.xml <在过程属性服务 / P>

<一个href="http://developer.android.com/guide/topics/manifest/service-element.html">http://developer.android.com/guide/topics/manifest/service-element.html

要报价:

  

的方法,其中该服务是要运行的名称。通常情况下,一个应用程序的所有组件在应用程序中创建的默认进程中运行。它具有相同的名称作为应用程序包。该元素的过程属性可以设置不同的默认为所有组件。但是组件可以覆盖默认有自己的进程属性,让你送$ P $垫在多个流程应用程序。

     

如果分配给该属性的名称以冒号(':'),一个新的进程,私有的应用程序,是在需要时创建和服务运行在这一进程。如果进程名以小写字母,该服务将在该名字的全局进程中运行,只要它有权这样做。这使得在不同应用程序的组件共享一个过程,减少资源的使用。

I have an Android app that consists of an activity and a service. Currently they both exist in the same process and use the same heap but I want have to separate process/heap for the service. Ie. I want the service to be completely independent of the activity so that if the activity crashes it won't affect the service. I do, however, want them to be installable as a single application. Is this possible?

解决方案

Definitely possible. See the process attribute for service in AndroidManifest.xml

http://developer.android.com/guide/topics/manifest/service-element.html

To quote:

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.

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process. If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.

这篇关于如何创建一个Android的活动和服务使用单独的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆