如何创建一个Android的活动和服务使用单独的进程创建一个、进程、和服务、Android

由网友(街角杂货店)分享简介:我有一个Android应用程序,包括活动和服务。目前,他们都存在于同一个进程,并使用同一个堆,但我希望有单独的进程/堆的服务。 IE浏览器。我希望该服务是完全独立于活动的,这样,如果该活动崩溃,也不会影响该服务。我这样做,不过,希望他们能够安装一个单一的应用程序。这可能吗?解决方案 绝对有可能。请参阅的Andr​​o...

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

解决方案

绝对有可能。请参阅的Andr​​oidManifest.xml

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

要报价:

  

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

     

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

Android学习 创建第一个活动

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.

阅读全文

相关推荐

最新文章