Android系统启动流程分析之启动应用 - ActivityManagerService

本文详细解析了Android系统中ActivityManagerService (AMS) 的启动过程,包括AMS的初始化、注册服务、启动Launcher应用等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ActivityManagerService(Ams)就是在initAndLoop方法里加载的.那么,看下initAndLoop方法里关于AMS的核心代码.

1.context = ActivityManagerService.main(factoryTest);


2.ActivityManagerService.setSystemProcess();


3.ActivityManagerService.installSystemProviders();


4.ActivityManagerService.self().setWindowManager(wm);


5.ActivityManagerService.self().systemReady


 一步一步进行分析.

1.context = ActivityManagerService.main(factoryTest);

这行代码是启动ActivityManagerService,获取上下文context,进入AMS的main方法看一下

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static final Context main(int factoryTest) {  
  2.         AThread thr = new AThread();  
  3.         thr.start();  
  4.           
  5.         synchronized (thr) {  
  6.             while (thr.mService == null) {  
  7.                 try {  
  8.                     //线程等待activitymanagerservice初始化完成  
  9.                     thr.wait();  
  10.                 } catch (InterruptedException e) {  
  11.                 }  
  12.             }  
  13.         }  
  14.         ActivityManagerService m = thr.mService;  
  15.         mSelf = m;  
  16.         //启动一个主线程  
  17.         ActivityThread at = ActivityThread.systemMain();  
  18.           
  19.         mSystemThread = at;  
  20.         //获取上下文context  
  21.         Context context = at.getSystemContext();  
  22.           
  23.         context.setTheme(android.R.style.Theme_Holo);  
  24.         m.mContext = context;  
  25.         m.mFactoryTest = factoryTest;  
  26.         m.mIntentFirewall = new IntentFirewall(m.new IntentFirewallInterface());  
  27.   
  28.         //新建一个activity堆栈管理辅助类  
  29.         m.mStackSupervisor = new ActivityStackSupervisor(m, context, thr.mLooper);  
  30.   
  31.         m.mBatteryStatsService.publish(context);  
  32.         m.mUsageStatsService.publish(context);  
  33.         m.mAppOpsService.publish(context);  
  34.   
  35.         synchronized (thr) {  
  36.             thr.mReady = true;  
  37.             //activitymanagerservice启动完成  
  38.             thr.notifyAll();  
  39.         }  
  40.   
  41.         m.startRunning(nullnullnullnull);  
  42.   
  43.         return context;  
  44.     }  

1.1

在mian方法里会创建一个线程AThread,AThread用来初始化Looper,AThread等待ActivityManagerService初始化完成后把自己的成员变量mService赋值给ActivityManagerService自身.

1.2 

启动一个主线程ActivityThread,ActivityThread是所有Application运行的主线程,

1.3

获取上下文context,最终是调用ContextImpl的createSystemContext方法返回的,context本质是ContextImpl的实例

1.4

等ActivityManagerService启动完成,调用m.startRunning()方法运行,在startRunning方法内部调用systemReady()方法.做系统准备工作.

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public final void startRunning(String pkg, String cls, String action, String data) {  
  2.         synchronized (this) {  
  3.             if (mStartRunning) {  
  4.                 return;  
  5.             }  
  6.             mStartRunning = true;  
  7.             mTopComponent = pkg != null && cls != null ? new ComponentName(pkg, cls) : null;  
  8.             //如果传入的action为空那么赋值Intent.ACTION_MAIN给mTopAction  
  9.             mTopAction = action != null ? action : Intent.ACTION_MAIN;  
  10.             mTopData = data;  
  11.             if (!mSystemReady) {  
  12.                 return;  
  13.             }  
  14.         }  
  15.   
  16.         systemReady(null);  
  17.     }  
这个mTopAction就是后面要启动第一个Activity,也就是Launcher的Action.

1.4.1

在systemReady方法调用mStackSupervisor.resumeTopActivitiesLocked方法.

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public void systemReady(final Runnable goingCallback){  
  2.             ......  
  3.             ......  
  4.             mStackSupervisor.resumeTopActivitiesLocked();  
  5.             ......  
  6.     }      
1.4.2

最终经过层层跳转会回到ActivityManagerService的startHomeActivityLocked方法.

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. boolean startHomeActivityLocked(int userId) {  
  2.         ......  
  3.         ......  
  4.         //获取intent信息  
  5.         Intent intent = getHomeIntent();  
  6.           
  7.         ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);  
  8.         if (aInfo != null) {  
  9.             intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));  
  10.             // Don't do this if the home app is currently being  
  11.             // instrumented.  
  12.             aInfo = new ActivityInfo(aInfo);  
  13.             aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);  
  14.             ProcessRecord app = getProcessRecordLocked(aInfo.processName, aInfo.applicationInfo.uid, true);  
  15.             if (app == null || app.instrumentationClass == null) {  
  16.                 intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);  
  17.                 //启动主程序,也就是Laucnher  
  18.                 mStackSupervisor.startHomeActivity(intent, aInfo);  
  19.             }  
  20.         }  
  21.         return true;  
  22.     }  
1.4.3

通过getHomeIntent方法获取Intent信息,看下代码

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Intent getHomeIntent() {  
  2.         Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null);  
  3.         intent.setComponent(mTopComponent);  
  4.         if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {  
  5.             //设置Category  
  6.             intent.addCategory(Intent.CATEGORY_HOME);  
  7.         }  
  8.         return intent;  
  9.     }  
给intent设置了Category,这个mTopAction就是前面设置的.

那么在这里Intent指定Action为action.MAIN,category为category.HOME.这正是启动Launcher的配置

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <intent-filter >  
  2.                 <action android:name="android.intent.action.MAIN" />  
  3.         <category android:name="android.intent.category.HOME" />  
  4.                  
  5.             </intent-filter>  
1.4.4

回到1.4.2,调用如下代码启动Launcher

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //启动主程序,也就是Laucnher  
  2. mStackSupervisor.startHomeActivity(intent, aInfo);  


2.ActivityManagerService.setSystemProcess()

这个方法是用来注册一些服务和获取、绑定进程信息的.

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static void setSystemProcess() {  
  2.         try {  
  3.             ActivityManagerService m = mSelf;  
  4.             //注册ActivityManagerService  
  5.             ServiceManager.addService(Context.ACTIVITY_SERVICE, m, true);  
  6.             //注册进程统计服务  
  7.             ServiceManager.addService(ProcessStats.SERVICE_NAME, m.mProcessStats);  
  8.             //注册内存服务  
  9.             ServiceManager.addService("meminfo"new MemBinder(m));  
  10.             //注册图像处理服务  
  11.             ServiceManager.addService("gfxinfo"new GraphicsBinder(m));  
  12.             //注册数据库服务  
  13.             ServiceManager.addService("dbinfo"new DbBinder(m));  
  14.             //注册cpu服务  
  15.             if (MONITOR_CPU_USAGE) {  
  16.                 ServiceManager.addService("cpuinfo"new CpuBinder(m));  
  17.             }  
  18.             //注册权限服务  
  19.             ServiceManager.addService("permission"new PermissionController(m));  
  20.             //获取应用信息  
  21.             ApplicationInfo info = mSelf.mContext.getPackageManager().getApplicationInfo("android", STOCK_PM_FLAGS);  
  22.             //绑定系统应用信息,  
  23.             mSystemThread.installSystemApplicationInfo(info);  
  24.   
  25.             synchronized (mSelf) {  
  26.                 //获取ProcessRecord实例,ProcessRecord是描述进程信息的  
  27.                 ProcessRecord app = mSelf.newProcessRecordLocked(info, info.processName, false);  
  28.                 app.persistent = true;  
  29.                 app.pid = MY_PID;  
  30.                 app.maxAdj = ProcessList.SYSTEM_ADJ;  
  31.                 //调用ProcessStatsService开始记录process的状态  
  32.                 app.makeActive(mSystemThread.getApplicationThread(), mSelf.mProcessStats);  
  33.                 //把进程名,uid,ProcessRecord实例存到mProcessNames数组中  
  34.                 mSelf.mProcessNames.put(app.processName, app.uid, app);  
  35.                 synchronized (mSelf.mPidsSelfLocked) {  
  36.                     mSelf.mPidsSelfLocked.put(app.pid, app);  
  37.                 }  
  38.                 mSelf.updateLruProcessLocked(app, falsenull);  
  39.                 mSelf.updateOomAdjLocked();  
  40.             }  
  41.         } catch (PackageManager.NameNotFoundException e) {  
  42.             throw new RuntimeException("Unable to find android system package", e);  
  43.         }  
  44.     }  
2.1

在ServiceManager里注册一些服务,AMS、进程统计服务、内存服务、图像处理服务、数据库服务、CPU服务、权限服务

2.2

通过PackageManager获取应用信息

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //获取应用信息  
  2. ApplicationInfo info = mSelf.mContext.getPackageManager().getApplicationInfo("android", STOCK_PM_FLAGS);  

获取包名为android的apk的信息,对应的就是framework-res.apk.

2.3

调用以下代码将获取到的应用信息绑定到mSystemThread的context上.

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. mSystemThread.installSystemApplicationInfo(info);  
2.4

ActivityManagerService调用newProcessRecordLocked方法创建一个ProcessRecord对象,ProcessRecord纪录了一个进程的信息,这里是指systemServer进程


3.ActivityManagerService.installSystemProviders();

这个方法是启动SettingsProvider的.SettingsProvider相当于系统的一个数据库.

4.ActivityManagerService.self().setWindowManager(wm);

这个方法是用来设置窗口管理器的.

5.ActivityManagerService.self().systemReady

这个方法是用来做系统准备工作的.

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ActivityManagerService.self().systemReady(new Runnable() {  
  2.             public void run() {  
  3.                 Slog.i(TAG, "Making services ready");  
  4.                 try {  
  5.                     // 开始监视native是否crash  
  6.                     ActivityManagerService.self().startObservingNativeCrashes();  
  7.                 } catch (Throwable e) {  
  8.                     reportWtf("observing native crashes", e);  
  9.                 }  
  10.                 if (!headless) {  
  11.                     //启动SystemUi  
  12.                     startSystemUi(contextF);  
  13.                 }  
  14.                 try {  
  15.                     if (mountServiceF != null)  
  16.                         mountServiceF.systemReady();  
  17.                 } catch (Throwable e) {  
  18.                     reportWtf("making Mount Service ready", e);  
  19.                 }  
  20.                 try {  
  21.                     if (batteryF != null)  
  22.                         batteryF.systemReady();  
  23.                 } catch (Throwable e) {  
  24.                     reportWtf("making Battery Service ready", e);  
  25.                 }  
  26.                 try {  
  27.                     if (networkManagementF != null)  
  28.                         networkManagementF.systemReady();  
  29.                 } catch (Throwable e) {  
  30.                     reportWtf("making Network Managment Service ready", e);  
  31.                 }  
  32.                 ......  
  33.                 ......  

5.1

开始监视native是否crash

5.2

启动SystemUi

5.3

做各种服务的准备工作,比如挂载管理服务、电脑管理服务、网络连接管理服务.


结束语:按照上述5大步来就可以慢慢理清启动应用的流程了.具体细节你们可以自己去探索.

原文地址: http://blog.youkuaiyun.com/qq_31530015/article/details/51745903


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值