在Android中每一个活动是一个过程,或一个应用程序是一个进程 [英] Every Activity in Android is a Process,or One Application is one process

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

问题描述

在此页面中,<一个href="http://androidapps.org.ua/androidintro_ipc.html">http://androidapps.org.ua/androidintro_ipc.html ,活动之间的切换意图被描述为进程间通信。现在我很困惑每一项活动是否是在一个应用程序中Android或全部活动的一个单独的进程是一个进程。我已经使用检查了所有的活动和服务的进程ID在我的应用程序:

 内部ID = android.os.Process.myPid();
 的System.out.println(流程活动1的编号:+ ID);
 

但它显示出相同的进程ID。 请回信。

解决方案

 所有在一个进程中运行的应用程序内的活动?
 

这取决于 Android的价值:过程属性的应用程序清单

如果属性安卓过程没有定义应用程序/活动标签清单中,默认情况下,所有活动将在单独的进程中运行(进程名是名字在清单中定义的包)

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.so.test安卓版code =1机器人:VERSIONNAME =1.0&GT;
    &LT;使用-SDK安卓的minSdkVersion =8/&GT;

    &lt;应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME&GT;
        &LT;活动机器人:名称=活动1&GT;
        &LT; /活性GT;
        &LT;活动机器人:名称=活性2&GT;
        &LT; /活性GT;
        &LT;活动机器人:名称=。Activity3&GT;
            &LT;意向滤光器&gt;
                &lt;作用机器人:名称=android.intent.action.MAIN/&GT;
                &LT;类机器人:名称=android.intent.category.LAUNCHER/&GT;
            &所述; /意图滤光器&gt;
        &LT; /活性GT;
    &LT; /用途&gt;
&LT; /舱单&GT;
 

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

 #PS
app_39 668 33 84492 20672 FFFFFFFF afd0c51c小号com.so.test
 

如果机器人:进程指定为活动新进程将使用相同​​的用户ID创建活动在这一过程中运行

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.so.test安卓版code =1机器人:VERSIONNAME =1.0&GT;
    &LT;使用-SDK安卓的minSdkVersion =8/&GT;

    &lt;应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME
     机器人:工艺=com.so.p1&GT;
        &LT;活动机器人:名称=活动1&GT;
        &LT; /活性GT;
        &LT;活动机器人:活性2名称=安卓程序=com.so.p2&GT;
        &LT; /活性GT;
        &LT;活动机器人:Activity3名称=安卓程序=com.so.p3&GT;
            &LT;意向滤光器&gt;
                &lt;作用机器人:名称=android.intent.action.MAIN/&GT;
                &LT;类机器人:名称=android.intent.category.LAUNCHER/&GT;
            &所述; /意图滤光器&gt;
        &LT; /活性GT;
    &LT; /用途&gt;
&LT; /舱单&GT;
 

如果清单定义如上

 活动1运行在com.so.p1过程
活性2运行在com.so.p2过程
Activity3运行在com.so.p3过程
 

在亚行的shell ps输出

 #PS
app_39 650 33 83192 20900 FFFFFFFF afd0c51c小号com.so.p1
app_39 659 33 83188 20864 FFFFFFFF afd0c51c小号com.so.p2
app_39 668 33 84492 20672 FFFFFFFF afd0c51c小号com.so.p3
 

如果一个活动需要在这个清单中没有定义的另一个进程中运行,两者的APK应该用同样的证书签名。

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);

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

解决方案

All activities inside an application run in one process?

It depends on value of android:process attribute in application 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>

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

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>

If the manifest is defined like above

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

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

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中每一个活动是一个过程,或一个应用程序是一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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