android:process 的用法 [英] Usage of android:process

查看:34
本文介绍了android:process 的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 AndroidManifest.xml 文件:

I have this AndroidManifest.xml file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1" android:versionName="1.0.0.0721"
android:process="com.lily.process" package="com.lily.test">

    <provider android:authorities="com.lily.test"
      android:name="com.lily.test.provider" 
      android:process="com.lily.process">
    </provider>

"android:process" 被添加为清单标签和提供者标签,我知道如果它被添加为提供者标签,提供者可以在com.lily.process"进程中运行.但是当它写成一个 manifest 标签时它的用途是什么?我已经尝试过了,但并非所有组件都可以在它识别的进程中运行.

"android:process" is added both as manifest tag and provider tag, I know if it is added as a provider tag, the provider can be run in the "com.lily.process" process. But what's the usage of it when written as a manifest tag? I have tried, but not all components could be running in the process it identified.

推荐答案

我同意没有多少人会发现 android:process 作为应用程序标签的一个属性很有用.但是,我发现它作为 activity 标记的属性很有用.

I would agree that not many people would find android:process to be useful as an attribute of the application tag. However, I have found it to be useful as an attribute of the activity tag.

android:process 在活动上的目的是指定您的活动应该在具有特定名称的进程中启动.选择该名称可用于将活动隔离在其自己的进程中(与启动它的进程不同),或者强制它与使用相同名称的其他活动共存于一个进程中.

The purpose of android:process on an activity is to specify that your activity should be launched in a process having a specific name. The choice of that name may be used either to isolate the activity in its own process (different from the one that launched it), or to force it to cohabit in a single process with other activities that use the same name.

根据开发指南(http://developer.android.com/guide/topics/manifest/activity-element.html):

"如果分配给此属性的名称以冒号 (':') 开头,则会在需要时创建应用程序私有的新进程并且活动在该进程中运行.如果进程名称以冒号开头小写字符,活动将在该名称的全局进程中运行,前提是它有这样做的权限.这允许不同应用程序中的组件共享一个进程,从而减少资源使用."

"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 activity runs in that process. If the process name begins with a lowercase character, the activity 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."

我最近发现此属性对于解决我在为应用启动帮助活动时遇到的问题很有用,在某些情况下,该应用相当接近仍适用于某些设备的 16MB 堆限制.在这些情况下,启动它的帮助活动是将我的应用推到极限,导致强制关闭.

I have recently found this attribute to be useful in solving a problem I had with launching a help activity for an app that, under certain circumstances, was fairly close to the 16MB heap limit that still applies to some devices. Launching its help activity was, in those situations, pushing my app over the limit, resulting in a force close.

通过使用 android:process 标记,我能够指定我的帮助活动应该在它自己的单独进程中启动.这个进程有自己的 16MB 堆,它不计入启动它的主应用程序的堆中.这永久且完全地防止了我的应用程序在启动帮助时耗尽堆空间和崩溃.

By using the android:process tag, I was able to specify that my help activity should be launched in a separate process of its own. This process had its own 16MB heap, and it was not counted against the heap of my main app that launched it. This permanently and completely prevented my app from running out of heap space and crashing when help was launched.

如果您的启动应用程序具有包名称

If your launching app has the package name

com.mycompany.mymainapp

并因此分配了一个相同字符串的进程名称,然后,如果您使用

and is therefore assigned a process name that is that same string, then, if you use

android:process=":myhelp"

在您启动的活动中,它将被分配进程名称

on your launched activity, it will be assigned the process name

com.mycompany.mymainapp:myhelp

并且该进程将拥有自己的独立进程 ID,您可以查看该 ID(例如在 DDMS 中).

and that process will have its own, separate process ID, which you can view (for example in DDMS).

至少,这是我的经验.到目前为止,我的测试是在运行 CM6(Android 2.2.1)的旧 Moto Droid 上进行的,配置为具有 16MB 的堆限制.

That, at least, has been my experience. My testing has so far been performed on an old Moto Droid running CM6 (Android 2.2.1), configured to have a 16MB heap limit.

就我而言,由于我不希望用户将帮助视为与我的应用程序分开,因此我包含了

In my case, since I did not want the user to perceive the help as being separate from my app, I included the

android:excludeFromRecents="true"

防止帮助活动出现在最近的应用程序(长按主页)列表中的属性.我也包括

attribute to prevent the help activity from appearing on the recent apps (long-press Home) list. I also included

android:taskAffinity="com.mycompany.mymainapp.HelpActivity"

其中 HelpActivity 是帮助活动的名称,用于将活动隔离在自己的任务中

where HelpActivity is the name of the help activity, to segregate the activity in its own task

我还补充了:

android:launchMode="singleInstance"

防止每次用户调用帮助时创建此应用的多个实例.

to prevent multiple instances of this app from being created each time the user invoked help.

我还添加了标志:

Intent.FLAG_ACTIVITY_NEW_TASK

用于启动帮助活动的 Intent.

to the Intent used to launch the help activity.

这些参数可能需要也可能不需要,具体取决于您对 android:process 属性的使用.

These parameters may or may not be needed, depending upon the use that you are making of the android:process attribute.

考虑到在为 Android 设备开发时遇到内存限制的频率,拥有一种技术可以在某些情况下允许您将应用程序的各个部分分解为单独的进程,每个进程都有自己的堆,这似乎是一份很棒的礼物.这样做可能存在我尚未考虑或经历过的隐患,但到目前为止,在我的特定情况下非常好.

Considering how often one encounters memory limits when developing for Android devices, having a technique that can, in some cases, allow you to break out parts of your app into separate processes, each with its own heap, seems like a wonderful gift. There may be hidden hazards in doing this that I have not yet considered or experienced, but so far, so good, in my particular instance.

这篇关于android:process 的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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