一、综述
上一篇讲述Android启动过程的时候,在第四部的关键进程SystemServer讲的很粗略,只是简单介绍它启动了很多服务,那么它究竟启动了哪些服务,执行过程是怎么样的呢?这一次,我就通过源代码注释的方式介绍一下SystemServer.
二、SystemServer源代码分析
/**入口
* The main entry point from zygote.
*/
public static void main(String[] args) {
new SystemServer().run();
}
/**
* 干杂事的线程
*/
private void run() {
// Prepare the main looper thread (this thread). 初始化looper,是不是没想到looper会被用在此处?
android.os.Process.setThreadPriority(
android.os.Process.THREAD_PRIORITY_FOREGROUND);
android.os.Process.setCanSelfBackground(false);
Looper.prepareMainLooper();
// Initialize native services 加载本地服务.
System.loadLibrary("android_servers");
nativeInit();
// 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);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
// Start services. 调用下面三方法启用各种服务
try {
startBootstrapServices();
startCoreServices();
startOtherServices();
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
}
}
/*
启动critical级别的服务,他们有复杂的依赖关系,顺序不能乱,自己想加服务,去别地
Starts the small tangle of critical services that are needed to get
* the system off the ground. These services have complex mutual dependencies
* which is why we initialize them all in one place here. Unless your service
* is also entwined in these dependencies, it should be initialized in one of
* the other functions.
*/
private void startBootstrapServices(){
//启动Installer
//启动ActivityManagerService
//启动PowerManagerService
//启动DisplayManagerService
//启动PackageManagerService
//启动UserManagerService
}
/* 启动一些关键服务*/
private void startCoreServices() {
//启动LightsService
//启动BatteryService
//启动UsageStatsService
//启动WebViewUpdateService
}
/*
启动各种各样的“杂项”服务
有一个关键标志变量 disableNonCoreServices 是否把非核心的服务禁用——————如果是,下面很多服务就不会启动了
Starts a miscellaneous grab bag of stuff that has yet to be refactored
* and organized.
*/
private void startOtherServices(){
//启动AccountManagerService 帐号管理
//启动ContentService 内容管理
//启动SystemProviders
//启动VibratorService 震动
//启动ConsumerIrService 远程控制周边设备
//启动AlarmManagerService 闹钟
//启动Watchdog
//启动WindowManagerService 窗口管理
//启动InputMethodManagerService 输入法
//截获用户输入,给一些额外反馈,view的点击、焦点事件分发
//启动AccessibilityManagerService
//启动MountService 磁盘加载服务
//启动LockSettingsService 锁屏、手势
//启动PersistentDataBlockService
//启动DevicePolicyManagerService //保证和API8兼容
//启动StatusBarManagerService 状态栏
//启动ClipboardService 剪切板
//启动NetworkManagementService 网络管理
//启动NetworkScoreService
//启动NetworkStatsService 网络统计
//启动NetworkPolicyManagerService 维护网络使用策略
//启动WifiService WIFI服务
//启动ConnectivityService 网络连接状态
//启动网络发现服务
//启动TextServicesManagerService 文本服务,如文本检查
//启动UpdateLockService
//启动LocationManagerService 位置
//启动CountryDetectorService 国家检测
//启动SearchManagerService 搜索
//启动DropBoxManagerService
//启动WallpaperManagerService 墙纸
//启动AudioService 音频
//启动USB服务
//WiredAccessoryManager 有线接入,耳机之类的
//启动SerialService 不知道是啥
//启动TwilightService 指明用户当前位置是否为晚上,配合调整夜间模式
//启动UiModeManagerService 界面模式
//启动JobSchedulerService 任务调度
//启动BackupManagerService 备份
//启动APPWIDGET_SERVICE 窗口小部件管理
//启动VOICE_RECOGNITION_MANAGER_SERVICE 声音重置服务
//启动DiskStatsService 硬盘统计服务
//启动NetworkTimeUpdateService 网络时间更新服务
//启动CommonTimeManagementService 时间管理服务
//启动DreamManagerService 屏幕保护
//负责将预加载的bitmap组装成纹理贴图,生成的纹理贴图可以被用来跨进程使用,以减少内存
//启动AssetAtlasService
//启动打印服务
//RestrictionsManagerService ?
//MediaSessionService ?
//HdmiControlService HDMI服务
//TvInputManagerService TV输入管理
//MediaRouterService ?
//TrustManagerService 信任管理
//FingerprintService 指纹
//BackgroundDexOptService ?
//LauncherAppsService
//MediaProjectionManagerService
//如果“安全” ,禁用JIT编译模式,不安全则开启JIT 何为安全????
// VMRuntime.getRuntime().disableJitCompilation()
//MmsServiceBroker ?
//接下来启动各种APP,省略……
}
static final void startSystemUi(Context context) {} //启动系统UI
三、小结
上面的代码是经过精简的,把目光聚焦于SystemServer究竟启动了什么服务,里面很多服务见名生义,就不多加解释了;其中很多打问号的服务,目前还没有弄清楚是干嘛的,随着学习会陆续更新。
如有错漏,请不吝赐教!