国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android應用框架之應用啟動過程詳解

2019-12-12 04:46:58
字體:
來源:轉載
供稿:網友

在Android的應用框架中,ActivityManagerService是非常重要的一個組件,盡管名字叫做ActivityManagerService,但通過之前的博客介紹,我們知道,四大組件的創建都是有AMS來完成的,其實不僅是應用程序中的組件,連Android應用程序本身也是AMS負責啟動的。AMS本身運行在一個獨立的進程中,當系統決定要在一個新的進程中啟動一個Activity或者Service時就會先啟動這個進程。而AMS啟動進程的過程是從startProcessLocked啟動的。

1.ActivityManagerService.startProcessLocked

public final class ActivityManagerService extends ActivityManagerNative     implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {   ......   private final void startProcessLocked(ProcessRecord app,         String hostingType, String hostingNameStr) {     ......     try {       int uid = app.info.uid;       int[] gids = null;       try {         gids = mContext.getPackageManager().getPackageGids(           app.info.packageName);       } catch (PackageManager.NameNotFoundException e) {         ......       }       ......       int debugFlags = 0;       ......       int pid = Process.start("android.app.ActivityThread",         mSimpleProcessManagement ? app.processName : null, uid, uid,         gids, debugFlags, null);       ......     } catch (RuntimeException e) {       ......     }   }   ...... } 

可以看到,函數會調用Process.start函數來創建一個進程,其中第一個參數”android.app.ActivityThread”是需要加載的類,而在完成這個類的加載之后就會運行ActivityThread.main函數。

2.Process.start

public class Process {  ......  public static final int start(final String processClass,    final String niceName,    int uid, int gid, int[] gids,    int debugFlags,    String[] zygoteArgs)  {    if (supportsProcesses()) {      try {        return startViaZygote(processClass, niceName, uid, gid, gids,          debugFlags, zygoteArgs);      } catch (ZygoteStartFailedEx ex) {        ......      }    } else {      ......      return 0;    }  }  ......}

這個函數最后會調用startViaZygote來創建進程,而Zygote正是Android孵化進程的服務,所有的進程都是通過Zygotefork出來的,所以這里創建進程的任務又落到了Zygote頭上了。

3.Process.startViaZygote

public class Process {  ......  private static int startViaZygote(final String processClass,      final String niceName,      final int uid, final int gid,      final int[] gids,      int debugFlags,      String[] extraArgs)      throws ZygoteStartFailedEx {    int pid;    synchronized(Process.class) {      ArrayList<String> argsForZygote = new ArrayList<String>();      // --runtime-init, --setuid=, --setgid=,      // and --setgroups= must go first      argsForZygote.add("--runtime-init");      argsForZygote.add("--setuid=" + uid);      argsForZygote.add("--setgid=" + gid);      if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {        argsForZygote.add("--enable-safemode");      }      if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {        argsForZygote.add("--enable-debugger");      }      if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {        argsForZygote.add("--enable-checkjni");      }      if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {        argsForZygote.add("--enable-assert");      }      //TODO optionally enable debuger      //argsForZygote.add("--enable-debugger");      // --setgroups is a comma-separated list      if (gids != null && gids.length > 0) {        StringBuilder sb = new StringBuilder();        sb.append("--setgroups=");        int sz = gids.length;        for (int i = 0; i < sz; i++) {          if (i != 0) {            sb.append(',');          }          sb.append(gids[i]);        }        argsForZygote.add(sb.toString());      }      if (niceName != null) {        argsForZygote.add("--nice-name=" + niceName);      }      argsForZygote.add(processClass);      if (extraArgs != null) {        for (String arg : extraArgs) {          argsForZygote.add(arg);        }      }      pid = zygoteSendArgsAndGetPid(argsForZygote);    }  }  ......}

函數里面最為重要的工作就是組裝argsForZygote參數,這些參數將告訴Zygote具體的啟動選項,例如”

主站蜘蛛池模板: 长治市| 凉山| 思南县| 通榆县| 巴林右旗| 巴东县| 桓台县| 抚宁县| 淳化县| 杭州市| 涟源市| 舒兰市| 寻乌县| 上饶市| 沾化县| 青州市| 宁海县| 齐齐哈尔市| 莱西市| 阿鲁科尔沁旗| 高密市| 沭阳县| 邢台市| 那坡县| 新河县| 邳州市| 利川市| 浦县| 繁峙县| 宣汉县| 石狮市| 醴陵市| 凭祥市| 封丘县| 通城县| 海门市| 涪陵区| 高要市| 青冈县| 象州县| 景洪市|