android系统启动时,System Server是android 基本服务的提供者,是android系统运行的最基本需求,所有service运行在system_server的进程中,system_server进程是android java虚拟机跑的第一个进程,从Zygote 创建而来。
class AndroidRuntime
{
public:
AndroidRuntime(char* argBlockStart, size_t argBlockSize);
virtual ~AndroidRuntime();
enum StartMode {
Zygote,
SystemServer,
Application,
Tool,
};
systemserver的main函数里面,直接开始起各种系统service
public static void main(String[] args) {
new SystemServer().run();
}
private void run() {
//系统不支持1970年前的时间设置
if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
}
//同步虚拟机的lib库,防止ota升级的时候把旧的remove掉
SystemProperties.set("persist.sys.dalvik.vm.lib.2", VMRuntime.getRuntime().vmLibrary());
// 清楚内存限制,扩大内存,和systemserver使用的堆大小
VMRuntime.getRuntime().clearGrowthLimit();
VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
// 兼容一些runtime fingerprint 的产品
Build.ensureFingerprintProperty();
// 使能环境变量需要用户id
Environment.setUserRequired(true);
// 确保系统binder调用都有前台优先权
BinderInternal.disableBackgroundScheduling(true);
android.os.Process.setThreadPriority(
android.os.Process.THREAD_PRIORITY_FOREGROUND);
android.os.Process.setCanSelfBackground(false);
//准备主循环
Looper.prepareMainLooper();
// 初始化native层的service
nativeInit();
// 关机失败检测
performPendingShutdown();
// 初始化系统上下文
createSystemContext();
// 创建了一个systemServiceManager
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
// Start services.
try {
//启动能使系统起飞的最小量的服务,这些服务有复杂的共同依赖
startBootstrapServices();
//
startCoreServices();
startOtherServices();
} catch (Throwable ex) {
if (mRecoveryManagerService != null && ex instanceof RuntimeException) {
mRecoveryManagerService.handleException((RuntimeException)ex, true);
}
}
//系统服务启动结束
addBootEvent(new String("Android:SysServerInit_END"));
// 无限循环
Looper.loop();
}
BootstrapService包含
//能够创建一些目录,如data/user,这个属于其他服务的前置服务。
Installer installer = mSystemServiceManager.startService(Installer.class);
//message服务
MessageMonitorService msgMonitorService = new MessageMonitorService();
AMS
mActivityManagerService = mSystemServiceManager.startService(ActivityManagerService.Lifecycle.class).getService();
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
PMS也需要早一点启动,因为需要处理进入系统的binder调用,同时能够验证这些调用的权限。PMS是通过AMS进行初始化的。
mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
mActivityManagerService.initPowerManagement();
DMS需要在package manager之前起来,为其提供显示需求。
mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);
mSystemServiceManager.startBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY);
recovery模式的话,通过属性看启动不启动RecoverManagerService
mRecoveryManagerService = new RecoveryManagerService(mSystemContext);
ServiceManager.addService(Context.RECOVERY_SERVICE, mRecoveryManagerService.asBinder());
mRecoveryManagerService.startBootMonitor();
最后再启动PackageManagerService
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
mFirstBoot = mPackageManagerService.isFirstBoot();
SystemProperties.set("persist.sys.firstboot", String.valueOf(mFirstBoot));
mPackageManager = mSystemContext.getPackageManager();
StartCoreService再启动一些跟bootStrap服务中相对独立的服务。
private void startCoreServices() {
// 管理LED灯和屏幕背光的服务
mSystemServiceManager.startService(LightsService.class);
// 电量服务
mSystemServiceManager.startService(BatteryService.class);
// 追踪应用使用情况服务
mSystemServiceManager.startService(UsageStatsService.class);
mActivityManagerService.setUsageStatsManager(
LocalServices.getService(UsageStatsManagerInternal.class));
mPackageManagerService.getUsageStatsIfNoPackageUsageInfo();
// check webview是否ready
mSystemServiceManager.startService(WebViewUpdateService.class);
}
startOtherServices启动其他服务,这里先关注我感兴趣的几个服务,这里的服务添加方式都是通过ServiceManager中添加service的方式注册服务。
private void startOtherServices() {
final Context context = mSystemContext;
AccountManagerService accountManager = null;
ContentService contentService = null;
VibratorService vibrator = null;
IAlarmManager alarm = null;
MountService mountService = null;
NetworkManagementService networkManagement = null;
NetworkStatsService networkStats = null;
NetworkPolicyManagerService networkPolicy = null;
ConnectivityService connectivity = null;
NetworkScoreService networkScore = null;
NsdService serviceDiscovery= null;
WindowManagerService wm = null;
BluetoothManagerService bluetooth = null;
UsbService usb = null;
SerialService serial = null;
NetworkTimeUpdateService networkTimeUpdater = null;
CommonTimeManagementService commonTimeMgmtService = null;
InputManagerService inputManager = null;
TelephonyRegistry telephonyRegistry = null;
ConsumerIrService consumerIr = null;
AudioService audioService = null;
MmsServiceBroker mmsService = null;
MobileManagerService mom = null;
MtkHdmiManagerService hdmiManager = null;
主要关注InputManager和WindowManager
//这里new了一个inputManager,并将这个inputManager关联到WMS中去,这里IMS和WMD是有一个对应关系的。
inputManager = new InputManagerService(context);
wm = WindowManagerService.main(context, inputManager,
mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL,
!mFirstBoot, mOnlyCore);
ServiceManager.addService(Context.WINDOW_SERVICE, wm);
ServiceManager.addService(Context.INPUT_SERVICE, inputManager);
//同时将这个wm设置到AMS中与之关联
mActivityManagerService.setWindowManager(wm);
//设置从IMS到WMS的回调
inputManager.setWindowManagerCallbacks(wm.getInputMonitor());
//启动inputManager,这一步很关键,inputmanager能够不断地从eventhub里面读取数据就是在这里启动了相应的线程。
inputManager.start();
网友评论