当前位置: 移动技术网 > 网络运营>服务器>Linux > ActivityManagerService(AMS)启动流程

ActivityManagerService(AMS)启动流程

2020年07月08日  | 移动技术网网络运营  | 我要评论

Android是基于Linux系统的,Linux启动后用户空间创建的第一个进程是init进程,而Zygote进程是init进程创建的,Zygote进程主要干两件事。

  • 1、孵化应用进程,我们app所在进程都是他fork出来的。
  • 2、创建SystemServer进程,我们常见的AMS、PMS、WMS等都是SystemServer进程中的服务。

而在SystemServer中会启动许多的服务,包括ActivityManagerService、PackageManagerService、PowerManagerService等服务,这里主要分析AMS的启动过程。

在Zygote进程创建了SystemServer进程之后,会执行SystemServer的mian方法:

SystemServer#main()
 public static void main(String[] args) {
        new SystemServer().run();
    }

在main方法中创建了SystemServer对象,并执行其run方法。

 SystemServer#run()
 private void run() {
    //引导服务:ActivityManagerService、PowerManagerService、PackageManagerService等服务
    startBootstrapServices();
    //其他服务
    startOtherServices();
}
 SystemServer#startBootstrapServices()  
 private void startBootstrapServices() {
      //1、创建ActivityManagerService(AMS)
    mActivityManagerService = mSystemServiceManager.startService(
                ActivityManagerService.Lifecycle.class).getService();
      //2、将服务添加到ServiceManager
      mActivityManagerService.setSystemProcess();
      
  }

注释1流程分析

SystemServiceManager#startService
public <T extends SystemService> T startService(Class<T> serviceClass) {
        try {
            final String name = serviceClass.getName();
            //...
            final T service;
            try {
                Constructor<T> constructor = serviceClass.getConstructor(Context.class);
                //通过反射创建ActivityManagerService.Lifecycle对象
                service = constructor.newInstance(mContext);
            } 
 			//又调用了startService(@NonNull final SystemService service)
            startService(service);
            //返回ActivityManagerService.Lifecycle对象
            return service;
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
        }
    }
 SystemServiceManager#startService
   public void startService(@NonNull final SystemService service) {
        // Register it.
        mServices.add(service);
        // Start it.
        long time = System.currentTimeMillis();
        try {
            //会调用ActivityManagerService的start()
            service.onStart();
        } 
    }
 public static final class Lifecycle extends SystemService {
       private final ActivityManagerService mService;
    
       public Lifecycle(Context context) {
          super(context);
          //创建了ActivityManagerService
          mService = new ActivityManagerService(context);
      }

      @Override
      public void onStart() {
          mService.start();
      }

      public ActivityManagerService getService() {
           //返回ActivityManagerService
          return mService;
      }
}

注释2流程分析

ActivityManagerService#setSystemProcess
public void setSystemProcess() {
        try {
        //将自己的binder注册到ServiceManager,谁要用就可以向ServiceManager发起请求获取到自己的binder从而和自己(AMS)通信
            ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);
          }
    }

最终在 startOtherServices()中还会调用ActivityManagerService的一个重要的方法
在SystemServer的startBootstrapServices()方法中启动的服务处理AMS之外,还有PKMS(PackageManagerService),但是现在不对它做讲解,只需知道PKMS启动后会遍历系统中所有的.apk文件、解析每个apk文件中的AndroidManifest.xml,并安装所有的应用程序(系统每次启动都会安装这些程序)。

然后AMS就会可以启动Launcher应用了(Launcher用来显示系统中所有的已经安装的程序,可以理解为桌面)

private void startOtherServices() {
   //....
	mActivityManagerService.systemReady(() -> {
		...
		//启动SystemUIService
		 startSystemUi(context, windowManagerF);
	},BOOT_TIMINGS_TRACE_LOG
}
 ActivityManagerService#systemReady
 public void systemReady(final Runnable goingCallback, BootTimingsTraceLog traceLog) {
     ......
     //启动桌面进程(可以看到桌面就是在这里启动的)
 	 startHomeActivityLocked(currentUserId, "systemReady");
 }
AMS
boolean startHomeActivityLocked(int userId, String reason) {
        //拿到Home页面的intent
        Intent intent = getHomeIntent();
        ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
        if (aInfo != null) {
            intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
            // Don't do this if the home app is currently being
            // instrumented.
            aInfo = new ActivityInfo(aInfo);
            aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
            ProcessRecord app = getProcessRecordLocked(aInfo.processName,
                    aInfo.applicationInfo.uid, true);
            if (app == null || app.instr == null) {
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
                final int resolvedUserId = UserHandle.getUserId(aInfo.applicationInfo.uid);
                // For ANR debugging to verify if the user activity is the one that actually
                // launched.
                final String myReason = reason + ":" + userId + ":" + resolvedUserId;
                //启动桌面
                mActivityStarter.startHomeActivityLocked(intent, aInfo, myReason);
            }
        } else {
            Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
        }

        return true;
    }

启动SystemUIService(和系统UI相关的服务)分析:

static final void startSystemUi(Context context, WindowManagerService windowManager) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.android.systemui",
                    "com.android.systemui.SystemUIService"));
        intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
        //Slog.d(TAG, "Starting service: " + intent);
        context.startServiceAsUser(intent, UserHandle.SYSTEM);
        windowManager.onSystemUiStarted();
    }

本文地址:https://blog.csdn.net/start_mao/article/details/107145357

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网