Android 中的每一个 Activity 都是一个进程,或者一个应用程序就是一个进程 [英] Every Activity in Android is a Process,or One Application is one process

查看:28
本文介绍了Android 中的每一个 Activity 都是一个进程,或者一个应用程序就是一个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此页面中,http://androidapps.org.ua/androidintro_ipc.html,活动之间的意图切换被描述为进程间通信.现在我很困惑是每个活动都是 android 中的一个单独进程,还是应用程序内的所有活动都是一个进程.我已经使用以下方法检查了我的应用程序中所有活动和服务的进程 ID:

In this page, http://androidapps.org.ua/androidintro_ipc.html , intent switching between activities is described as Inter Process Communication. Now I am confused whether every activity is a separate process in android or All activities inside an application is one process. I have checked the process id of all activities and service in my Application using:

 int id = android.os.Process.myPid();
 System.out.println("Process id of Activity1 :"+id);

但它显示相同的进程ID.请回复.

But it is showing same process id. Please reply back.

推荐答案

All activities inside an application run in one process?

这取决于应用程序清单中 android:process 属性的值.

It depends on value of android:process attribute in application manifest.

如果在 manifest 中没有为 Application/Activity 标签定义属性 android:process,默认情况下所有 Activity 将在单个进程中运行(进程名称将是 manifest 中定义的包的名称)

if attribute android:process is not defined for Application/Activity tags in the manifest, by default all the activities will run in single process (process name will be name of the package defined in manifest)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.so.test" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Activity1">
        </activity>
        <activity android:name=".Activity2">
        </activity>
        <activity android:name=".Activity3">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

在上面的清单中,所有活动都在进程 com.so.test 中运行,adb shell 中的 ps 命令输出:

In the above manifest all activities run in process com.so.test,ps command output in adb shell:

# ps
app_39    668   33    84492  20672 ffffffff afd0c51c S com.so.test

如果为 Activity 指定了 android:process,将使用相同的用户 ID 创建新进程,并且该 Activity 在该进程中运行.

If android:process is specified for Activity new process will be created with the same userid and the activity runs in that process.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.so.test" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name" 
     android:process="com.so.p1">
        <activity android:name=".Activity1">
        </activity>
        <activity android:name=".Activity2" android:process="com.so.p2">
        </activity>
        <activity android:name=".Activity3" android:process="com.so.p3">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

如果清单定义如上

Activity1 runs in com.so.p1 process
Activity2 runs in com.so.p2 process
Activity3 runs in com.so.p3 process

adb shell 中的 ps 输出

ps output in adb shell

# ps
app_39    650   33    83192  20900 ffffffff afd0c51c S com.so.p1
app_39    659   33    83188  20864 ffffffff afd0c51c S com.so.p2
app_39    668   33    84492  20672 ffffffff afd0c51c S com.so.p3

如果 Activity 需要在此清单中未定义的另一个进程中运行,则两个 APK 应使用相同的证书进行签名.

If an Activity needs to be run in another process not defined in this manifest, both APKs should be signed with the same certificate.

这篇关于Android 中的每一个 Activity 都是一个进程,或者一个应用程序就是一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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