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

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

问题描述

我有一个包含活动和服务的 Android 应用.目前它们都存在于同一个进程中并使用相同的堆,但我想必须为服务分离进程/堆.IE.我希望服务完全独立于活动,这样即使活动崩溃也不会影响服务.但是,我确实希望它们可以作为单个应用程序安装.这可能吗?

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?

推荐答案

绝对有可能.查看AndroidManifest.xml

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

引用:

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

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 Activity 和 Service的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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