SystemServer分析
一 概述
SystemServer跟Zygote一样,是android中最重要的二个进程,是android中java世界的二大支柱;它们是相辅相成的,任何一个进程崩溃了都会导致android java世界的崩溃;因为java进程都是Zygote孵化出来的,zygote进程死了,其它java进程也就死了;如果这二个进程真的死了,则Linux系统中的进程init会重新启动以重新建立android java世界;通过ps命令我们可以看出,SystemServer在系统中的进程名为”system_server”,它和系统服务有着重要关系,它承载着framework的核心服务,如ActivityManageServer,PowerManagerService等。
1.1 SystemServer.main
system_server进程也是zygote进程孵化出来的,当zygote进程成功孵化出system_server进程后会调用SystemServer.main()方法
public final class SystemServer { ... public static void main(String[] args) { //先初始化SystemServer对象,再调用对象的run()方法 [见1.2] new SystemServer().run(); }}
1.2 SystemServer.run
private void run() { try { //初始化系统时间 if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) { Slog.w(TAG, "System clock is before 1970; setting to 1970."); SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME); } ...... //主线程初始化loop Looper.prepareMainLooper(); //加载android_servers.so库,该库包含的源码在frameworks/base/services/目录下 System.loadLibrary("android_servers"); //检测上次关机过程是否失败 performPendingShutdown(); // 初始化上下方环境 [见1.3] createSystemContext(); //创建系统服务管理 mSystemServiceManager = new SystemServiceManager(mSystemContext); LocalServices.addService(SystemServiceManager.class, mSystemServiceManager); } finally { Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); } // 启动各种服务 try { Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartServices"); startBootstrapServices(); //[见1.6] startCoreServices();//[见1.7] startOtherServices();//[见1.8] } catch (Throwable ex) { Slog.e("System", "******************************************"); Slog.e("System", "************ Failure starting system services", ex); throw ex; } finally { Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); } //开启loop循环 Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited"); }
主要是初始化上下文环境,初始化各种核心服务
1.3 SystemServer.createSystemContext
private void createSystemContext() { //创建system_server进程运行的上下文环境 [见1.4] ActivityThread activityThread = ActivityThread.systemMain(); mSystemContext = activityThread.getSystemContext(); //设置主题 mSystemContext.setTheme(DEFAULT_SYSTEM_THEME); }
通过ActivityThread这个类创建system_server进程运行的环境,这个类也是app进程创建成功后app运行的初始化类,在app进程创建成功后zygote会调用ActivityThread的main方法,进行app的初始化
1.4 ActivityThread.systemMain
[—>ActivityThread.java]
public static ActivityThread systemMain() { ...... ActivityThread thread = new ActivityThread(); //[见1.5] thread.attach(true); return thread; }
1.5 ActivityThread.systemMain
private void attach(boolean system) { sCurrentActivityThread = this; mSystemThread = system; //非系统服务进入,app初始化时会进入 if (!system) { ...... } else { //系统服务调用进入到这里 android.ddm.DdmHandleAppName.setAppName("system_process", UserHandle.myUserId()); try { mInstrumentation = new Instrumentation(); ContextImpl context = ContextImpl.createAppContext( this, getSystemContext().mPackageInfo); mInitialApplication = context.mPackageInfo.makeApplication(true, null); mInitialApplication.onCreate(); } catch (Exception e) { throw new RuntimeException( "Unable to instantiate Application():" + e.toString(), e); } } ...... @Override public void onLowMemory() { } @Override public void onTrimMemory(int level) { } }); }
这里还是初始化环境,创建Instrumentation,ContextImpl,LoadedApk,Application等重要核心类
1.6 SystemServer.startBootstrapServices
private void startBootstrapServices() { ...... //启动服务ActivityManagerService mActivityManagerService = mSystemServiceManager.startService( ActivityManagerService.Lifecycle.class).getService(); mActivityManagerService.setSystemServiceManager(mSystemServiceManager); mActivityManagerService.setInstaller(installer); //启动服务PowerManagerService mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class); //初始化power management mActivityManagerService.initPowerManagement(); //启动服务LightsService mSystemServiceManager.startService(LightsService.class); //启动服务DisplayManagerService mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class); ...... //启动服务PackageManagerService mPackageManagerService = PackageManagerService.main(mSystemContext, installer, mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore); mFirstBoot = mPackageManagerService.isFirstBoot(); mPackageManager = mSystemContext.getPackageManager(); //启动服务UserManagerService,新建目录/data/user/ ServiceManager.addService(Context.USER_SERVICE, UserManagerService.getInstance()); AttributeCache.init(mSystemContext); //设置AMS,这个方法将ams添加到ServiceManager管理器中 mActivityManagerService.setSystemProcess(); //启动传感器服务 startSensorService();}
1.7 SystemServer.startCoreServices
private void startCoreServices() { //启动服务BatteryService,用于统计电池电量,需要LightService. mSystemServiceManager.startService(BatteryService.class); //启动服务UsageStatsService,用于统计应用使用情况 mSystemServiceManager.startService(UsageStatsService.class); mActivityManagerService.setUsageStatsManager( LocalServices.getService(UsageStatsManagerInternal.class)); mPackageManagerService.getUsageStatsIfNoPackageUsageInfo(); //启动服务WebViewUpdateService mSystemServiceManager.startService(WebViewUpdateService.class);}
1.8 SystemServer.startOtherServices
private void startOtherServices() { ... watchdog.init(context, mActivityManagerService); inputManager = new InputManagerService(context); // input wm = WindowManagerService.main(...); // window inputManager.start(); //启动input mDisplayManagerService.windowManagerAndInputReady(); ... mSystemServiceManager.startService(MOUNT_SERVICE_CLASS); // mount mPackageManagerService.performBootDexOpt(); // dexopt操作 ActivityManagerNative.getDefault().showBootMessage(...); //显示启动界面 ... statusBar = new StatusBarManagerService(context, wm); //statusBar //dropbox ServiceManager.addService(Context.DROPBOX_SERVICE, new DropBoxManagerService(context, new File("/data/system/dropbox"))); mSystemServiceManager.startService(JobSchedulerService.class); //JobScheduler lockSettings.systemReady(); mSystemServiceManager.startBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY); mSystemServiceManager.startBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); ... // 准备好window, power, package, display服务 wm.systemReady(); mPowerManagerService.systemReady(...); mPackageManagerService.systemReady(); mDisplayManagerService.systemReady(...); mActivityManagerService.systemReady(...); }
到此,system_server进程的工作完成,进入Looper.loop状态,等待其它线程通过handler发送消息到主线程进行处理。
二 服务类别
system_server进程中的服务类别可以划分为引导服务,核心服务,其它服务三类,所有系统服务多达80余种
- 引导服务:ActivityManagerService,PowerManagerService,LightService,DisplayManagerService,PackageManagerService,UserManagerService,SensorService;
- 核心服务:BatterService,UsageStatsService,WebViewUpdateService;
- 其它服务:AlarmManagerService,VibratorService等
更多相关文章
- Android:AIDL进程之间的通信
- Android 系统框架介绍
- Android线程与进程(二)线程详解
- Android五个进程等级
- Android开机向导setupwizard,设置系统语言,WiFi向导
- Android 4 / iMX6系统开发手记