android 應(yīng)用啟動流程分析(三)

android 應(yīng)用啟動流程分析(二)

android啟動總覽.png

上篇介紹了進程存在的情況下應(yīng)用的啟動流程,該篇繼續(xù)分享進程不存在時的流程

應(yīng)用進程不存在的情況下應(yīng)用進程的孵化

先上ActivityStackSupervisor 的startSpecificActivityLocked 的開始代碼


    void startSpecificActivityLocked(ActivityRecord r,
            boolean andResume, boolean checkConfig) {
            
            // 進程存在先改,直接啟動activity
            
            // 進程不存在啟動進程
            
          
        mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
                "activity", r.intent.getComponent(), false, false, true);
            
            }


在ActivityManagerService中一路調(diào)用最終調(diào)用startProcessLocked 多參方法


 private final void startProcessLocked(ProcessRecord app, String hostingType,
            String hostingNameStr, String abiOverride, String entryPoint, String[] entryPointArgs) {
            if (hostingType.equals("webview_service")) {
                startResult = startWebView(entryPoint,
                        app.processName, uid, uid, gids, debugFlags, mountExternal,
                        app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,
                        app.info.dataDir, null, entryPointArgs);
            } else {
            // 啟動進程
                startResult = Process.start(entryPoint,
                        app.processName, uid, uid, gids, debugFlags, mountExternal,
                        app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,
                        app.info.dataDir, invokeWith, entryPointArgs);
            }
            
            }

調(diào)用zygoteProcess 的啟動進程的方法

    public static final ProcessStartResult start(final String processClass,
                                  final String niceName,
                                  int uid, int gid, int[] gids,
                                  int debugFlags, int mountExternal,
                                  int targetSdkVersion,
                                  String seInfo,
                                  String abi,
                                  String instructionSet,
                                  String appDataDir,
                                  String invokeWith,
                                  String[] zygoteArgs) {
        return zygoteProcess.start(processClass, niceName, uid, gid, gids,
                    debugFlags, mountExternal, targetSdkVersion, seInfo,
                    abi, instructionSet, appDataDir, invokeWith, zygoteArgs);
    }

ZygoteProcess 一路調(diào)用最終調(diào)用zygoteSendArgsAndGetResult, 生產(chǎn)出一個process并標(biāo)識

 /**
     * Sends an argument list to the zygote process, which starts a new child
     * and returns the child's pid. Please note: the present implementation
     * replaces newlines in the argument list with spaces.
     *
     * @throws ZygoteStartFailedEx if process start failed for any reason
     */
    @GuardedBy("mLock")
    private static Process.ProcessStartResult zygoteSendArgsAndGetResult(
            ZygoteState zygoteState, ArrayList<String> args)
            throws ZygoteStartFailedEx {
        try {
            // Throw early if any of the arguments are malformed. This means we can
            // avoid writing a partial response to the zygote.
            int sz = args.size();
            for (int i = 0; i < sz; i++) {
                if (args.get(i).indexOf('\n') >= 0) {
                    throw new ZygoteStartFailedEx("embedded newlines not allowed");
                }
            }

            /**
             * See com.android.internal.os.SystemZygoteInit.readArgumentList()
             * Presently the wire format to the zygote process is:
             * a) a count of arguments (argc, in essence)
             * b) a number of newline-separated argument strings equal to count
             *
             * After the zygote process reads these it will write the pid of
             * the child or -1 on failure, followed by boolean to
             * indicate whether a wrapper process was used.
             */
            final BufferedWriter writer = zygoteState.writer;
            final DataInputStream inputStream = zygoteState.inputStream;

            writer.write(Integer.toString(args.size()));
            writer.newLine();

            for (int i = 0; i < sz; i++) {
                String arg = args.get(i);
                writer.write(arg);
                writer.newLine();
            }

            writer.flush();

            // Should there be a timeout on this?
            Process.ProcessStartResult result = new Process.ProcessStartResult();

            // Always read the entire result from the input stream to avoid leaving
            // bytes in the stream for future process starts to accidentally stumble
            // upon.
            result.pid = inputStream.readInt();
            result.usingWrapper = inputStream.readBoolean();

            if (result.pid < 0) {
                throw new ZygoteStartFailedEx("fork() failed");
            }
            return result;
        } catch (IOException ex) {
            zygoteState.close();
            throw new ZygoteStartFailedEx(ex);
        }
    }

此次android應(yīng)用的分享到此先暫時告一段落这橙, 別忘了關(guān)注和點贊哦

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末柱宦,一起剝皮案震驚了整個濱河市总寒,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌特占,老刑警劉巖糙置,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異是目,居然都是意外死亡谤饭,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來揉抵,“玉大人亡容,你說我怎么就攤上這事≡┙瘢” “怎么了闺兢?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長戏罢。 經(jīng)常有香客問我屋谭,道長,這世上最難降的妖魔是什么龟糕? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任桐磁,我火速辦了婚禮,結(jié)果婚禮上讲岁,老公的妹妹穿的比我還像新娘我擂。我一直安慰自己,他們只是感情好缓艳,可當(dāng)我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布扶踊。 她就那樣靜靜地躺著,像睡著了一般郎任。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上备籽,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天舶治,我揣著相機與錄音,去河邊找鬼车猬。 笑死霉猛,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的珠闰。 我是一名探鬼主播惜浅,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼伏嗜!你這毒婦竟也來了坛悉?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤承绸,失蹤者是張志新(化名)和其女友劉穎裸影,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體军熏,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡轩猩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片均践。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡晤锹,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出彤委,到底是詐尸還是另有隱情鞭铆,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布葫慎,位于F島的核電站衔彻,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏偷办。R本人自食惡果不足惜艰额,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望椒涯。 院中可真熱鬧柄沮,春花似錦、人聲如沸废岂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽湖苞。三九已至拯欧,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間财骨,已是汗流浹背镐作。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留隆箩,地道東北人该贾。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像捌臊,于是被迫代替她去往敵國和親杨蛋。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內(nèi)容