8. 9.
ServiceManager.addService(\, m);
10. ServiceManager.addService(\, new MemBinder(m)); 11. if (MONITOR_CPU_USAGE) {
12. ServiceManager.addService(\, new CpuBinder(m)); 13. }
14. ServiceManager.addService(\, new PermissionController(m)); 15.
16. ApplicationInfo info =
17. mSelf.mContext.getPackageManager().getApplicationInfo( 18. \, STOCK_PM_FLAGS);
19. mSystemThread.installSystemApplicationInfo(info); 20.
21. synchronized (mSelf) {
22. ProcessRecord app = mSelf.newProcessRecordLocked( 23. mSystemThread.getApplicationThread(), info, 24. info.processName); 25. app.persistent = true; 26. app.pid = MY_PID; 27. app.maxAdj = SYSTEM_ADJ;
28. mSelf.mProcessNames.put(app.processName, app.info.uid, app); 29. synchronized (mSelf.mPidsSelfLocked) { 30. mSelf.mPidsSelfLocked.put(app.pid, app); 31. }
32. mSelf.updateLruProcessLocked(app, true, true); 33. }
34. } catch (PackageManager.NameNotFoundException e) { 35. throw new RuntimeException(
36. \, e); 37. } 38. } 39. ...... 40. }
这个函数首先是将这个ActivityManagerService实例添加到ServiceManager中去托管,这样其它地方就可以通过ServiceManager.getService接口来访问这个全局唯一的ActivityManagerService实例了,接着又通过调用mSystemThread.installSystemApplicationInfo函数来把应用程序框架层下面的android包加载进来 ,这里的mSystemThread是一个ActivityThread类型的实例变量,它是在上面的Step 7中创建的,后面就是一些其它的初始化工作了。
Step 10. ActivityManagerService.systemReady
这个函数是在上面的Step 6中的ServerThread.run函数在将系统中的一系列服务都初始化完毕之后才调用的,它定义在frameworks/base/services/java/com/android/server/am/ActivityManagerServcie.java文件中:
view plain
1. 2. 3. 4. 5. 6. 7. 8. 9.
public final class ActivityManagerService extends ActivityManagerNative implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { ......
public void systemReady(final Runnable goingCallback) { ......
synchronized (this) { ......
10.
11. mMainStack.resumeTopActivityLocked(null); 12. } 13. } 14.
15. ...... 16. }
这个函数的内容比较多,这里省去无关的部分,主要关心启动Home应用程序的逻辑,这里就是通过mMainStack.resumeTopActivityLocked函数来启动Home应用程序的了,这里的mMainStack是一个ActivityStack类型的实例变量。
Step 11. ActivityStack.resumeTopActivityLocked
这个函数定义在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:
view plain
1. 2. 3. 4. 5. 6. 7. 8. 9.
public class ActivityStack { ......
final boolean resumeTopActivityLocked(ActivityRecord prev) { // Find the first activity that is not finishing. ActivityRecord next = topRunningActivityLocked(null);
......
10. if (next == null) {
11. // There are no more activities! Let's just start up the 12. // Launcher... 13. if (mMainStack) {
14. return mService.startHomeActivityLocked(); 15. } 16. } 17.
18. ...... 19. } 20.
21. ...... 22. }
这里调用函数topRunningActivityLocked返回的是当前系统Activity堆栈最顶端的Activity,由于此时还没有Activity被启动过,因此,返回值为null,即next变量的值为null,于是就调用mService.startHomeActivityLocked语句,这里的mService就是前面在Step 7中创建的ActivityManagerService实例了。
Step 12. ActivityManagerService.startHomeActivityLocked 这个函数定义在
frameworks/base/services/java/com/android/server/am/ActivityManagerServcie.java文件中:
view plain
1. 2. 3. 4. 5. 6. 7. 8. 9.
public final class ActivityManagerService extends ActivityManagerNative implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { ......
boolean startHomeActivityLocked() { ......
Intent intent = new Intent( mTopAction,
10. mTopData != null ? Uri.parse(mTopData) : null); 11. intent.setComponent(mTopComponent);
12. if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { 13. intent.addCategory(Intent.CATEGORY_HOME); 14. }
15. ActivityInfo aInfo =
16. intent.resolveActivityInfo(mContext.getPackageManager(), 17. STOCK_PM_FLAGS); 18. if (aInfo != null) {
19. intent.setComponent(new ComponentName(
20. aInfo.applicationInfo.packageName, aInfo.name)); 21. // Don't do this if the home app is currently being 22. // instrumented.
23. ProcessRecord app = getProcessRecordLocked(aInfo.processName, 24. aInfo.applicationInfo.uid);
25. if (app == null || app.instrumentationClass == null) {
26. intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); 27. mMainStack.startActivityLocked(null, intent, null, null, 0, aInfo, 28. null, null, 0, 0, 0, false, false); 29. } 30. } 31.
32. return true;
33. } 34.
35. ...... 36. }
函数首先创建一个CATEGORY_HOME类型的Intent,然后通过Intent.resolveActivityInfo函数向PackageManagerService查询Category类型为HOME的Activity,这里我们假设只有系统自带的Launcher应用程序注册了HOME类型的Activity(见packages/apps/Launcher2/AndroidManifest.xml文件):
view plain
1. 2. 3. 4. 5. 6. 7. 8. 9.
xmlns:android=\ package=\ android:sharedUserId=\ > ...... 10. android:name=\ 11. android:process=\ 12. android:label=\ 13. android:icon=\> 14. 15. 16. android:name=\ 17. android:launchMode=\ 18. android:clearTaskOnLaunch=\ 19. android:stateNotNeeded=\ 20. android:theme=\ 21. android:screenOrientation=\ 22. android:windowSoftInputMode=\> 23. 24. 31. ...... 32.