The AMS Framework learning learning

A, AMS startup process

  1. SystemServer.java inside the boot main () function

    public static void main(String[] args) {

       new SystemServer().run(); }

  2. SystemServer.java which run method

   . System loadLibrary ( "android_servers");     // load the dynamic link library service  

   startBootstrapServices (); // start ActivityManagerService, PowerManagerService, PackageManagerService guide and other services
   startCoreServices (); // start the core services
   startOtherServices (); // start other services

 

  3. SystemServiceManager.java of startService

     public <T extends SystemService> T startService(Class<T> serviceClass) {

      ......

      Constructor<T> constructor = serviceClass.getConstructor(Context.class);//1

      service = constructor.newInstance(mContext);//2      constructor的newInstance方法来创建Lifecycle类型的service对象   

      mServices.add(service);//3                   service添加到ArrayList类型的mServices对象中来完成注册

      service.onStart();//4                        service(Lifecycle)的onStart方法来启动service,并返回该service

      .......}

    

     LifecycleIt is actually an inner class of ActivityManagerService.java

      public static final class Lifecycle extends SystemService {

      private final ActivityManagerService mService;

      public Lifecycle(Context context) {

      super(context); mService = new ActivityManagerService(context);//1}

      @Override public void onStart() {

      mService.start();//2 }      

      public ActivityManagerService getService() {

      return mService;//3 } }

    When you call Lifecycle type of service of onStart method, the method is actually called a start at 2 AMS comments

    

  Two, AMS and the process starts

    AMS when you start the application checks the application required by the application process exists, there will be a request Zygote process will require the application process starts. Calls bringUpServiceLocked method ActiveServices the start of the process of Service

private String bringUpServiceLocked(ServiceRecord r, int intentFlags, boolean execInFg, boolean whileRestarting, boolean permissionsReviewRequired) throws TransactionTooLargeException { ... final String procName = r.processName;//1 ProcessRecord app; if (!isolated) { app = mAm.getProcessRecordLocked(procName, r.appInfo.uid, false);//2 if (DEBUG_MU) Slog.v(TAG_MU, "bringUpServiceLocked: appInfo.uid=" + r.appInfo.uid + " app=" + app); if (app != null && app.thread != null) {//3 try { app.addPackage(r.appInfo.packageName, r.appInfo.versionCode, mAm.mProcessStats); realStartServiceLocked(r, app, execInFg);//4 return null; } catch (TransactionTooLargeException e) { ... } } else { app = r.isolatedProc; } if (app == null && !permissionsReviewRequired) {//5 if ((app=mAm.startProcessLocked(procName, r.appInfo, true, intentFlags, "service", r.name, false, isolated, false)) == null) {//6 ... } if (isolated) { r.isolatedProc = app; } } ... }
   1.ServiceRecord的processName的值赋值给procName ,其中ServiceRecord用来描述Service的android:process属性
   2.将procName和Service的uid传入到AMS的getProcessRecordLocked方法中,来查询是否存在一个与Service对应的ProcessRecord类型的对象app,ProcessRecord主要用来记录运行的应用程序进程的信息
   5.判断Service对应的app为null则说明用来运行Service的应用程序进程不存在,
   6.AMS的startProcessLocked方法来创建对应的应用程序进程


    

 

Guess you like

Origin www.cnblogs.com/liunx1109/p/11104216.html