Android系统启动流程

Android系统启动流程

简单描述:

  • 1 按下电源键开机,启动芯片代码,启动一个BootLoader小程序。
  • 2 BootLoader主要的功能是完成系统的设置,启动init.rc,init.cpp,init.zygote32.rc(8.0以后)
  • 3 init 启动zygote:通过rc文件,service.cpp找到相关的name,对应的main方法。即app_main.cpp的main方法。
  • 4 zygote启动SystemServer
    Zygote开启了Android世界。
    app_main.cpp是启动zygote进程的方法。
    AndroidRuntime.start() ----(启动虚拟机,)
    启动ZygoteInit的main方法执行。主要做了以下四件事
  1. 创建一个Server端的socket(LocalServerSocket)。
  2. 预加载类和资源。
  3. 启动SystemServer进程。
  4. 等待AMS请求创建新的应用程序进程。
    启动SystemServer, —> RuntimeInit.zygoteInit
    —>启动Binder线程池,这样就可以和其他进程通讯了,nativeZygoteInit();
    —>执行SystemServer的main方法,applicationInit(targetSdkVersion, argv, classLoader)
  • 5 SystemServer启动主线程,以及其他的系统服务。
    开启looper,创建mSystemServiceManager,启动服务器的小助手,
    启动引导服务,核心服务,其他服务
引导服务installer
引导服务ActivityManagerService赋值install,systemServiceManager
引导服务PowerManagerServiceinitPowerManager
引导服务LightsService启动服务
引导服务DisplayManagerService在packagemanagerservice之前提供
引导服务PackageManagerService是不是第一次boot,获取packagemanager
引导服务UserManagerService
引导服务AttributeCache.init(mSystemContext)
引导服务mActivityManagerService.setSystemProcess()
引导服务startSensorService();

服务太多了,不一一描述了。
服务的启动有两种,一种是SystemServiceManager创建并启动的服务,都是继承了SystemService,但是并没有实现IBinder,所以是不可以进行Binder通信的,也就是由它管理的服务是用于进程内部通信的。
另一种是ServiceManager.addService(“package”, m);之类的。
ServiceManager是用来管理系统中各种Service的,由它管理的服务都实现了IBinder,所以在ServiceManager中注册的服务是用于进程间通信的:用于系统C/S架构中的Binder通信机制。客户端要使用某个Service,需要先到ServiceManager中查询Service的信息,然后根据该信息与Service所在的Server进程建立通信,这样客户端就可以使用Service了。

SystemServer进程主要做一下几件事

  1. 启动Binder线程池,这样就可以与其他进程进行通信了。
  2. 创建SystemServiceManager,用于对系统其他服务进行创建、启动和声明周期管理。
  3. 启动各种系统服务。

Launcher启动过程

SystemServer启动主线程,以及其他的系统服务。有一个方法startOtherService

mActivityManagerService.systemReady(new Runnable() {
		public void run(){
			
		}
		 
}

由ActivityManagerService的systemReady。

public void systemReady(final Runnable goingCallback) {
    //省略
	startHomeActivityLocked(currentUserId, "systemReady");
	//省略
}
    boolean startHomeActivityLocked(int userId, String reason) {
    	//判断系统的运行模式和mTopAction的值
        if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
                && mTopAction == null) {
            // We are running in factory test mode, but unable to find
            // the factory test app, so just sit around displaying the
            // error message and don't try to start anything.
            return false;
        }
		//获取Luncher启动所需要的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.instrumentationClass == null) {
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
				//启动Luncher
                mActivityStarter.startHomeActivityLocked(intent, aInfo, reason);
            }
        } else {
            Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
        }

        return true;
    }

https://www.jianshu.com/p/4dfc0b67d92f

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值