開始進(jìn)入Framework的源碼分析,在分析源碼之前,先來了解一下android系統(tǒng)啟動的流程,如下圖所示:

在Zygote進(jìn)程中會孵化一個SystemServer進(jìn)程,在這個SystemServer進(jìn)程中完成了大量的系統(tǒng)服務(wù)的啟動,這些系統(tǒng)服務(wù)啟動完成后才啟動桌面Launcher進(jìn)程,那么這個SystemServer又是怎么啟動的。
SystemServer的啟動由于需要啟動大量的系統(tǒng)服務(wù),所以每次開機(jī)都需要耗時較長才會顯示桌面。
啟動過程:
在進(jìn)入源碼分析之前,先來根據(jù)下面的思維導(dǎo)圖了解一下整體流程,按照執(zhí)行順序進(jìn)行了排序。

1、準(zhǔn)備主線程Looper
說明:
1、SystemServer的啟動從其Main方法執(zhí)行;創(chuàng)建SystemServer實例調(diào)用其run方法;
2、配置系統(tǒng)時間、時區(qū)、語言等信息;
3、準(zhǔn)備主線程Looper
//SystemServer.java
public static void main(String[] args) {
new SystemServer().run();
}
private void run() {
try {
// Record the process start information in sys props.
SystemProperties.set(SYSPROP_START_COUNT, String.valueOf(mStartCount));
SystemProperties.set(SYSPROP_START_ELAPSED, String.valueOf(mRuntimeStartElapsedTime));
SystemProperties.set(SYSPROP_START_UPTIME, String.valueOf(mRuntimeStartUptime));
if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
Slog.w(TAG, "System clock is before 1970; setting to 1970.");
SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
}
//
// Default the timezone property to GMT if not set.
//設(shè)置時間
String timezoneProperty = SystemProperties.get("persist.sys.timezone");
if (timezoneProperty == null || timezoneProperty.isEmpty()) {
Slog.w(TAG, "Timezone not set; setting to GMT.");
SystemProperties.set("persist.sys.timezone", "GMT");
}
// If the system has "persist.sys.language" and friends set, replace them with
// "persist.sys.locale". Note that the default locale at this point is calculated
// using the "-Duser.locale" command line flag. That flag is usually populated by
// AndroidRuntime using the same set of system properties, but only the system_server
// and system apps are allowed to set them.
//
// NOTE: Most changes made here will need an equivalent change to
// core/jni/AndroidRuntime.cpp
if (!SystemProperties.get("persist.sys.language").isEmpty()) {
final String languageTag = Locale.getDefault().toLanguageTag();
SystemProperties.set("persist.sys.locale", languageTag);
SystemProperties.set("persist.sys.language", "");
SystemProperties.set("persist.sys.country", "");
SystemProperties.set("persist.sys.localevar", "");
}
// The system server should never make non-oneway calls
Binder.setWarnOnBlocking(true);
// The system server should always load safe labels
PackageItemInfo.forceSafeLabels();
// Default to FULL within the system server.
SQLiteGlobal.sDefaultSyncMode = SQLiteGlobal.SYNC_MODE_FULL;
// Deactivate SQLiteCompatibilityWalFlags until settings provider is initialized
SQLiteCompatibilityWalFlags.init(null);
// Here we go!
Slog.i(TAG, "Entered the Android system server!");
int uptimeMillis = (int) SystemClock.elapsedRealtime();
EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, uptimeMillis);
if (!mRuntimeRestart) {
MetricsLogger.histogram(null, "boot_system_server_init", uptimeMillis);
}
// In case the runtime switched since last boot (such as when
// the old runtime was removed in an OTA), set the system
// property so that it is in sync. We can | xq oqi't do this in
// libnativehelper's JniInvocation::Init code where we already
// had to fallback to a different runtime because it is
// running as root and we need to be the system user to set
// the property. http://b/11463182
SystemProperties.set("persist.sys.dalvik.vm.lib.2", VMRuntime.getRuntime().vmLibrary());
// Mmmmmm... more memory!
VMRuntime.getRuntime().clearGrowthLimit();
// The system server has to run all of the time, so it needs to be
// as efficient as possible with its memory usage.
VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
// Some devices rely on runtime fingerprint generation, so make sure
// we've defined it before booting further.
Build.ensureFingerprintProperty();
// Within the system server, it is an error to access Environment paths without
// explicitly specifying a user.
Environment.setUserRequired(true);
// Within the system server, any incoming Bundles should be defused
// to avoid throwing BadParcelableException.
BaseBundle.setShouldDefuse(true);
// Within the system server, when parceling exceptions, include the stack trace
Parcel.setStackTraceParceling(true);
// Ensure binder calls into the system always run at foreground priority.
BinderInternal.disableBackgroundScheduling(true);
// Increase the number of binder threads in system_server
BinderInternal.setMaxThreads(sMaxBinderThreads);
// Prepare the main looper thread (this thread).
android.os.Process.setThreadPriority(
android.os.Process.THREAD_PRIORITY_FOREGROUND);
android.os.Process.setCanSelfBackground(false);
//這里開始準(zhǔn)備主線程的Looper
Looper.prepareMainLooper();
Looper.getMainLooper().setSlowLogThresholdMs(
SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);
// Initialize native services.
System.loadLibrary("android_servers");
// Debug builds - allow heap profiling.
if (Build.IS_DEBUGGABLE) {
initZygoteChildHeapProfiling();
}
// Check whether we failed to shut down last time we tried.
// This call may not return.
performPendingShutdown();
// Initialize the system context.
createSystemContext();
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
mSystemServiceManager.setStartInfo(mRuntimeRestart,
mRuntimeStartElapsedTime, mRuntimeStartUptime);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
// Prepare the thread pool for init tasks that can be parallelized
SystemServerInitThreadPool.get();
} finally {
traceEnd(); // InitBeforeStartServices
}
2、加載nativeService:
// Initialize native services.
System.loadLibrary("android_servers");
3、創(chuàng)建systemContext
- 創(chuàng)建ActivityThread實例;
- 獲取systemContext、systemUiContext;
- 設(shè)置系統(tǒng)theme
// Initialize the system context.
createSystemContext();
private void createSystemContext() {
//創(chuàng)建ActivityThread
ActivityThread activityThread = ActivityThread.systemMain();
//獲取systemContext,并設(shè)置theme
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
//獲取systemUiContenx ,設(shè)置theme
final Context systemUiContext = activityThread.getSystemUiContext();
systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
}
3.1 創(chuàng)建ActivityThread

//ActivityThread.java
public final class ActivityThread extends ClientTransactionHandler {
public static ActivityThread systemMain() {
// The system process on low-memory devices do not get to use hardware
// accelerated drawing, since this can add too much overhead to the process.
if (!ActivityManager.isHighEndGfx()) {
ThreadedRenderer.disable(true);
} else {
ThreadedRenderer.enableForegroundTrimming();
}
//1.這里new一個ActivityThread出來,通過構(gòu)造方法可以明白是獲得了資源管理器實例
ActivityThread thread = new ActivityThread();
//2.這里的attach第一個參數(shù)是true,
thread.attach(true, 0);
return thread;
}
//構(gòu)造方法
ActivityThread() {
mResourcesManager = ResourcesManager.getInstance();
}
private void attach(boolean system, long startSeq) {
sCurrentActivityThread = this;
mSystemThread = system;
if (!system) {
//注意由于第一個參數(shù)這時傳遞進(jìn)來的true ,所以不會進(jìn)入該分支
} else {
android.ddm.DdmHandleAppName.setAppName("system_process",
UserHandle.myUserId());
try {
//1.1 創(chuàng)建Instrumentation隊列,并將當(dāng)前的ActivityThead賦值給instrumentation的成員變量
mInstrumentation = new Instrumentation();
mInstrumentation.basicInit(this);
//1.2 創(chuàng)建app上下文
ContextImpl context = ContextImpl.createAppContext(this, getSystemContext().mPackageInfo);
//1.3
mInitialApplication = context.mPackageInfo.makeApplication(true, null);
mInitialApplication.onCreate();
} catch (Exception e) {
throw new RuntimeException(
"Unable to instantiate Application():" + e.toString(), e);
}
}
//..........
}
//1.2.1 獲取systemContext
public ContextImpl getSystemContext() {
synchronized (this) {
if (mSystemContext == null) {
//1.2.1.1 創(chuàng)建systemContext
mSystemContext = ContextImpl.createSystemContext(this);
}
return mSystemContext;
}
}
}
//ContextImpl.java
class ContextImpl extends Context {
//1.2.1.2創(chuàng)建systemContext
static ContextImpl createSystemContext(ActivityThread mainThread) {
//1.2.1.2.1進(jìn)入LoadedApk的構(gòu)造方法:創(chuàng)建applicationInfo,系統(tǒng)資源、類加載器等
LoadedApk packageInfo = new LoadedApk(mainThread);
ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, 0,
null, null);
//1.2.1.2.2 給context設(shè)置資源
context.setResources(packageInfo.getResources());
context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
context.mResourcesManager.getDisplayMetrics());
return context;
}
private ContextImpl(@Nullable ContextImpl container, @NonNull ActivityThread mainThread,
@NonNull LoadedApk packageInfo, @Nullable String splitName,
@Nullable IBinder activityToken, @Nullable UserHandle user, int flags,
@Nullable ClassLoader classLoader, @Nullable String overrideOpPackageName) {
mOuterContext = this;
// If creator didn't specify which storage to use, use the default
// location for application.
if ((flags & (Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE
| Context.CONTEXT_DEVICE_PROTECTED_STORAGE)) == 0) {
final File dataDir = packageInfo.getDataDirFile();
if (Objects.equals(dataDir, packageInfo.getCredentialProtectedDataDirFile())) {
flags |= Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
} else if (Objects.equals(dataDir, packageInfo.getDeviceProtectedDataDirFile())) {
flags |= Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
}
}
mMainThread = mainThread;
mActivityToken = activityToken;
mFlags = flags;
if (user == null) {
user = Process.myUserHandle();
}
mUser = user;
mPackageInfo = packageInfo;
mSplitName = splitName;
mClassLoader = classLoader;
mResourcesManager = ResourcesManager.getInstance();
String opPackageName;
if (container != null) {
mBasePackageName = container.mBasePackageName;
opPackageName = container.mOpPackageName;
setResources(container.mResources);
mDisplay = container.mDisplay;
} else {
mBasePackageName = packageInfo.mPackageName;
ApplicationInfo ainfo = packageInfo.getApplicationInfo();
if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
// Special case: system components allow themselves to be loaded in to other
// processes. For purposes of app ops, we must then consider the context as
// belonging to the package of this process, not the system itself, otherwise
// the package+uid verifications in app ops will fail.
opPackageName = ActivityThread.currentPackageName();
} else {
opPackageName = mBasePackageName;
}
}
mOpPackageName = overrideOpPackageName != null ? overrideOpPackageName : opPackageName;
mContentResolver = new ApplicationContentResolver(this, mainThread);
}
//1.2.2 創(chuàng)建app上線文
static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo,
String opPackageName) {
if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
//1.2.2.1
ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, 0,
null, opPackageName);
context.setResources(packageInfo.getResources());
return context;
}
}
//LoadedApk.java
public final class LoadedApk {
LoadedApk(ActivityThread activityThread) {
mActivityThread = activityThread;
mApplicationInfo = new ApplicationInfo();
mApplicationInfo.packageName = "android";
mPackageName = "android";
mAppDir = null;
mResDir = null;
mSplitAppDirs = null;
mSplitResDirs = null;
mSplitClassLoaderNames = null;
mOverlayDirs = null;
mDataDir = null;
mDataDirFile = null;
mDeviceProtectedDataDirFile = null;
mCredentialProtectedDataDirFile = null;
mLibDir = null;
mBaseClassLoader = null;
mSecurityViolation = false;
mIncludeCode = true;
mRegisterPackage = false;
mResources = Resources.getSystem();
mDefaultClassLoader = ClassLoader.getSystemClassLoader();
mAppComponentFactory = createAppFactory(mApplicationInfo, mDefaultClassLoader);
mClassLoader = mAppComponentFactory.instantiateClassLoader(mDefaultClassLoader,
new ApplicationInfo(mApplicationInfo));
}
//1.3.1
public Application makeApplication(boolean forceDefaultAppClass,
Instrumentation instrumentation) {
if (mApplication != null) {
return mApplication;
}
Application app = null;
String appClass = mApplicationInfo.className;
if (forceDefaultAppClass || (appClass == null)) {
appClass = "android.app.Application";
}
try {
java.lang.ClassLoader cl = getClassLoader();
if (!mPackageName.equals("android")) {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
"initializeJavaContextClassLoader");
initializeJavaContextClassLoader();
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
}
ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);
//1.3.1.1
app = mActivityThread.mInstrumentation.newApplication(
cl, appClass, appContext);
appContext.setOuterContext(app);
} catch (Exception e) {
if (!mActivityThread.mInstrumentation.onException(app, e)) {
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
throw new RuntimeException(
"Unable to instantiate application " + appClass
+ ": " + e.toString(), e);
}
}
mActivityThread.mAllApplications.add(app);
mApplication = app;
if (instrumentation != null) {
try {
//1.3.1.2
instrumentation.callApplicationOnCreate(app);
} catch (Exception e) {
if (!instrumentation.onException(app, e)) {
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
throw new RuntimeException(
"Unable to create application " + app.getClass().getName()
+ ": " + e.toString(), e);
}
}
}
return app;
}
}
3.2 getSystemContext()
這一部分其實和3.1中獲取systemContext是一樣的步驟,這里就不在啰嗦了。
4、創(chuàng)建systemServiceManager,準(zhǔn)備線程池初始化任務(wù)
//SystemServer.java
mSystemServiceManager = new SystemServiceManager(mSystemContext);
mSystemServiceManager.setStartInfo(mRuntimeRestart, mRuntimeStartElapsedTime, mRuntimeStartUptime);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
SystemServerInitThreadPool.start();
5、啟動服務(wù)
5.1、啟動引導(dǎo)服務(wù)
引導(dǎo)服務(wù)主要有常見的:看門狗、AMS ,installer安裝器、PKMS應(yīng)用包管理器,電量管理PMS等引導(dǎo)服務(wù),詳細(xì)細(xì)節(jié)看下面的源碼:
注:下面的源碼筆者進(jìn)行了刪減,僅保留了一些常見的服務(wù)及其執(zhí)行的順序
//SystemServer.java
//這里是啟動引導(dǎo)服務(wù)
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
/**
啟動watchDog
首先啟動看門狗,是為了在引導(dǎo)服務(wù)期間發(fā)生死鎖時是系統(tǒng)服務(wù)奔潰
*/
final Watchdog watchdog = Watchdog.getInstance();
watchdog.start();
/**
>>>>>>>>創(chuàng)建安裝器installer ,用于PKMS安裝應(yīng)用程序使用
*/
Installer installer = mSystemServiceManager.startService(Installer.class);
// AMS創(chuàng)建啟動
ActivityTaskManagerService atm = mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class).getService();
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
mWindowManagerGlobalLock = atm.getGlobalLock();
//PMS 電量管理服務(wù)
mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
//命令終端管理器
mSystemServiceManager.startService(ThermalManagerService.class);
//初始化電量管理PowerManager
mActivityManagerService.initPowerManagement();
// Manages LEDs and display backlight so we need it to bring up the display.
mSystemServiceManager.startService(LightsService.class);
//顯示管理器需要在包管理器啟動之前提供顯示指標(biāo)
mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);
// 啟動顯示管理器
mSystemServiceManager.startBootPhase(t, SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY);
//啟動PKMS
Watchdog.getInstance().pauseWatchingCurrentThread("packagemanagermain");
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
// 現(xiàn)在包管理器已經(jīng)啟動,請注冊dex load reporter以捕獲任何系統(tǒng)服務(wù)器加載的dex文件。
// 這些dex文件將由BackgroundDexOptService進(jìn)行優(yōu)化。
SystemServerDexLoadReporter.configureSystemServerDexReporter(mPackageManagerService);
mPackageManager = mSystemContext.getPackageManager();
// 初始化用于緩存包中資源的屬性緩存。
AttributeCache.init(mSystemContext);
// 為系統(tǒng)進(jìn)程設(shè)置應(yīng)用程序?qū)嵗㈤_始。
mActivityManagerService.setSystemProcess();
//看門狗的設(shè)置
watchdog.init(mSystemContext, mActivityManagerService);
// 初始化:管理覆蓋包服務(wù)
mSystemServiceManager.startService(new OverlayManagerService(mSystemContext));
}
5.2、啟動核心服務(wù)
//SystemServer.java
private void startCoreServices(@NonNull TimingsTraceAndSlog t) {
// 初始化系統(tǒng)配置服務(wù)
mSystemServiceManager.startService(SystemConfigService.class);
// 初始化燈光服務(wù),獲取電池電量等
mSystemServiceManager.startService(BatteryService.class);
// 啟動應(yīng)用程序使用統(tǒng)計信息服務(wù)
mSystemServiceManager.startService(UsageStatsService.class);
mActivityManagerService.setUsageStatsManager(LocalServices.getService(UsageStatsManagerInternal.class));
// 跟蹤可更新WebView是否處于就緒狀態(tài),并監(jiān)視更新安裝。
if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_WEBVIEW)) {
mWebViewUpdateService = mSystemServiceManager.startService(WebViewUpdateService.class);
}
//跟蹤并緩存設(shè)備狀態(tài)。
mSystemServiceManager.startService(CachedDeviceStateService.class);
// 跟蹤在Binder調(diào)用中花費的cpu時間
mSystemServiceManager.startService(BinderCallsStatsService.LifeCycle.class);
//跟蹤處理程序中處理消息所花費的時間。.
mSystemServiceManager.startService(LooperStatsService.Lifecycle.class);
//管理apk回滾
mSystemServiceManager.startService(ROLLBACK_MANAGER_SERVICE_CLASS);
// 用于捕獲錯誤報告的服務(wù).
mSystemServiceManager.startService(BugreportManagerService.class);
//GPU和GPU驅(qū)動程序的服務(wù)
mSystemServiceManager.startService(GpuService.class);
}
5.3、啟動其他服務(wù)
在這個其他服務(wù)中包含了很多我們需要使用到的服務(wù),本文下面僅列出一部分常見的服務(wù),更多細(xì)節(jié)見源碼
//SystemServer.java
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
try {
mActivityManagerService.installSystemProviders();
vibrator = new VibratorService(context);
ServiceManager.addService("vibrator", vibrator);
mSystemServiceManager.startService(new AlarmManagerService(context));
inputManager = new InputManagerService(context);
// WMS(Window Manager Service)啟動過程
wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,
new PhoneWindowManager(), mActivityManagerService.mActivityTaskManager);
ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false,
DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
ServiceManager.addService(Context.INPUT_SERVICE, inputManager,
/* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);
//給AMS設(shè)置WindowManagerService
mActivityManagerService.setWindowManager(wm);
//wms 初始化
wm.onInitReady();
if (!isWatch && enableVrService) {
//VR管理服務(wù)
mSystemServiceManager.startService(VrManagerService.class);
}
inputManager.setWindowManagerCallbacks(wm.getInputManagerCallback());
inputManager.start();
// TODO: Use service dependencies instead.
mDisplayManagerService.windowManagerAndInputReady();
if (mFactoryTestMode == FactoryTest.FACTORY_TEST_LOW_LEVEL) {
} else if (!context.getPackageManager().hasSystemFeature
(PackageManager.FEATURE_BLUETOOTH)) {
} else {
//啟動藍(lán)牙服務(wù)
mSystemServiceManager.startService(BluetoothService.class);
}
} catch (Throwable e) {
throw e;
}
if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
if (!isWatch) {
try {
//狀態(tài)欄管理服務(wù)
statusBar = new StatusBarManagerService(context);
ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);
} catch (Throwable e) {
}
}
try {
//網(wǎng)絡(luò)管理服務(wù)
networkManagement = NetworkManagementService.create(context);
ServiceManager.addService(Context.NETWORKMANAGEMENT_SERVICE, networkManagement);
} catch (Throwable e) {
reportWtf("starting NetworkManagement Service", e);
}
if (context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_WIFI)) {
//Wifi相關(guān)的服務(wù):掃描wifi
mSystemServiceManager.startService(WIFI_SERVICE_CLASS);
mSystemServiceManager.startService(
"com.android.server.wifi.scanner.WifiScanningService");
}
//狀態(tài)欄的通知服務(wù)
mSystemServiceManager.startService(NotificationManagerService.class);
SystemNotificationChannels.removeDeprecated(context);
SystemNotificationChannels.createAll(context);
notification = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
//定位管理服務(wù)
mSystemServiceManager.startService(LocationManagerService.Lifecycle.class);
if (context.getResources().getBoolean(R.bool.config_enableWallpaperService)) {
//桌面壁紙管理服務(wù)
mSystemServiceManager.startService(WALLPAPER_SERVICE_CLASS);
}
if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)
|| mPackageManager.hasSystemFeature(
PackageManager.FEATURE_USB_ACCESSORY)
|| isEmulator) {
// USB服務(wù)
mSystemServiceManager.startService(USB_SERVICE_CLASS);
}
// TODO(aml-jobscheduler): Think about how to do it properly.
//JobScheduler
mSystemServiceManager.startService(JOB_SCHEDULER_SERVICE_CLASS);
try {
//runtime服務(wù)
ServiceManager.addService("runtime", new RuntimeService(context));
} catch (Throwable e) {
reportWtf("starting RuntimeService", e);
}
if (hasFeatureFace) {
//人臉識別的服務(wù)
mSystemServiceManager.startService(FaceService.class);
}
}
if (!disableCameraService) {
//相機(jī)服務(wù)代理
mSystemServiceManager.startService(CameraServiceProxy.class);
}
// Permission policy service
//權(quán)限管理策略服務(wù)
mSystemServiceManager.startService(PermissionPolicyService.class);
}
6、Looper開始工作
SystemServer在run中執(zhí)行啟動一些列服務(wù)后,最后啟動Looper進(jìn)入Handler的消息永動機(jī)中開始工作。關(guān)于Handler另作分析。
總結(jié):
通過上述源碼分析,我們可以清楚的知道,SystemServer的啟動過程:
準(zhǔn)備主線程的Looper ,
加載NativeSystem ,加載好之后創(chuàng)建SystemContext提供經(jīng)常允許的環(huán)境等;
創(chuàng)建SystemServiceManager,并準(zhǔn)備線程池來進(jìn)行初始化任務(wù);
創(chuàng)建并啟動一系列的服務(wù)(引導(dǎo)服務(wù),核心服務(wù),其他服務(wù))
最后Looper永動機(jī)開始工作,這樣SystemServer就啟動完成了。
ActivityThread的創(chuàng)建過程,需要特別注意,而且非常重要。
在上面的源碼中,我們可以看到大部分的服務(wù)是通過SystemServerManager來啟動的,那么要詳情了解具體一個Server的啟動就要從SystemServerManager的startService()開始了。