framework——ATMS啟動流程

little_fat_sheep發表於2020-12-20

1 前言

 

2 ATMS 啟動流程

(1)step 1:main()

/frameworks/base/services/java/com/android/server/SystemServer.java

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

(2)step 2:run()

/frameworks/base/services/java/com/android/server/SystemServer.java

private void run() {
	try {
		...
		// 建立Looper
		Looper.prepareMainLooper();
		// 載入libandroid_servers.so
		System.loadLibrary("android_servers");
		// 建立系統的 Context
		createSystemContext();
		// 建立 SystemServiceManager
		mSystemServiceManager = new SystemServiceManager(mSystemContext);
		LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
		...
	}
	...
	try {
		//啟動引導服務,ActivityManagerService、PackageManagerService、PowerManagerService 等
		startBootstrapServices(); 
		//啟動核心服務,BatteryService、UsageStatusService 等
		startCoreServices(); 
		//啟動其他服務,WindowManagerService、CameraService、AlarmManagerService 等
		startOtherServices(); 
		...
	}
	...
	// 開啟訊息迴圈
	Looper.loop();
}

(3)step 3:startBootstrapServices()

/frameworks/base/services/java/com/android/server/SystemServer.java

private void startBootstrapServices() {
	...
    //啟動 ATMS
	ActivityTaskManagerService atm = mSystemServiceManager.startService(ActivityTaskManagerService.Lifecycle.class).getService();
    //啟動 AMS,並將 ATMS 注入
	mActivityManagerService = ActivityManagerService.Lifecycle.startService(mSystemServiceManager, atm);
	...
}

(4)step 4:startService()

/frameworks/base/services/core/java/com/android/server/SystemServiceManager.java

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);
			service = constructor.newInstance(mContext);
		}
		...
		startService(service);
		return service;
	}
	...
}

public void startService(SystemService service) {
	mServices.add(service); //mServices: ArrayList<SystemService>
	...
	try {
		service.onStart();
	}
	...
}

(5)Lifecycle

/frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.Lifecycle.java

public static final class Lifecycle extends SystemService {
	private final ActivityTaskManagerService mService;

	public Lifecycle(Context context) {
		super(context);
		mService = new ActivityTaskManagerService(context);
	}

	public void onStart() {
		publishBinderService(Context.ACTIVITY_TASK_SERVICE, mService);
		mService.start();
	}
	
	...

	public ActivityTaskManagerService getService() {
		return mService;
	}
}

3 ATMS 初始化

 

4 ATMS 初始化

 

相關文章