您的位置:首页 > 移动开发 > Android开发

Android 内核 - 05 SystemServer

2016-03-27 21:57 351 查看

概要

SystemServer 由 ZygoteInit 创建的第一个子进程,负责系统管理

framework\base\services\java\com\android\server\SystemServer.java

SystemServer.java中其实包含了两个 class, SystemServer 和 ServerThread。
SystemServer 负责提供 main() 接口,ServerThread是工作的实体,虽然名字中有个Thread,但它并没有创建一个Thread。

1, SystemServer main()

SystemServer main函数的工作内容:

设置优先级

SystemProperties.set("persist.sys.dalvik.vm.lib", VMRuntime.getRuntime().vmLibrary());

设置时间

SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);

SamplingProfilerIntegration.start()

开启定时任务 writeSnapshot

timer.schedule(new TimerTask() {
@Override
public void run() {
SamplingProfilerIntegration.writeSnapshot("system_server", null);
}
}, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);

// Mmmmmm... more memory!
dalvik.system.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);

加载 android_servers
System.loadLibrary("android_servers");

nativeInit();

开始 ServerThread,这里虽然用了一个Thread的名字,但其实他并不是一个Thread
// This used to be its own separate thread, but now it is

// just the loop we run on the main thread.

ServerThread thr = new ServerThread();
thr.initAndLoop();

2, ServerThread 的内容

ServerThread 中主要涉及了系统中各种服务的管理,内容很丰富。看看它的成员变量就知道了:

Installer installer = null;
AccountManagerService accountManager = null; (联系人)账户管理
ContentService contentService = null; ContentProvider服务,跨进程数据交换
LightsService lights = null; 自然光感应传感器服务
PowerManagerService power = null; 电源管理服务
DisplayManagerService display = null; 显示管理服务
BatteryService battery = null; 电池管理服务
VibratorService vibrator = null; 震动服务
AlarmManagerService alarm = null; 定时服务提醒
MountService mountService = null; 挂载服务
NetworkManagementService networkManagement = null; 网络管理
NetworkStatsService networkStats = null; 网络状态管理
NetworkPolicyManagerService networkPolicy = null; 网络策略管理
ConnectivityService connectivity = null; 网络连接管理
WifiP2pService wifiP2p = null; Wifip2p服务
WifiService wifi = null; Wifi服务
NsdService serviceDiscovery= null; 网络服务发现服务管理(Network Service Discovery Service)
IPackageManager pm = null; 程序包管理服务
Context context = null; Context
WindowManagerService wm = null; 窗口管理服务
BluetoothManagerService bluetooth = null; 蓝牙管理服务
DockObserver dock = null; Monitors for a docking station,针对扩展槽的服务
UsbService usb = null; USB管理服务
SerialService serial = null; 串口管理服务
TwilightService twilight = null; 指出用户当前所在位置是否为晚上,可以用来调整夜间模式
UiModeManagerService uiMode = null; 管理UI的模式 例如(夜间模式和行车模式)
RecognitionManagerService recognition = null; 身份识别服务
NetworkTimeUpdateService networkTimeUpdater = null; 网络时间更新服务
CommonTimeManagementService commonTimeMgmtService = null; 主要是配置 native Common Time service
InputManagerService inputManager = null; 输入法管理服务
TelephonyRegistry telephonyRegistry = null; 电话服务,注册电话模块事件响应
ConsumerIrService consumerIr = null; 红外管理服务,可以通过红外远程控制其他电子产品

在 ServerThread中最主要的工作就是创建一个服务,并把它加入ServiceManager中。
这样代码中的逻辑并不复杂,主要就是创建服务的过程。

例如电源管理:

power = new PowerManagerService();
ServiceManager.addService(Context.POWER_SERVICE, power);

显示管理:

display = new DisplayManagerService(context, wmHandler);
ServiceManager.addService(Context.DISPLAY_SERVICE, display, true);

当各个服务的对象创建完成后,这些对象也会暴露出来供外层使用。
// These are needed to propagate to the runnable below.

final Context contextF = context;
final MountService mountServiceF = mountService;
final BatteryService batteryF = battery;
final NetworkManagementService networkManagementF = networkManagement;
final NetworkStatsService networkStatsF = networkStats;
final NetworkPolicyManagerService networkPolicyF = networkPolicy;
final ConnectivityService connectivityF = connectivity;
final DockObserver dockF = dock;
final UsbService usbF = usb;
final TwilightService twilightF = twilight;
final UiModeManagerService uiModeF = uiMode;
final AppWidgetService appWidgetF = appWidget;
final WallpaperManagerService wallpaperF = wallpaper;
final InputMethodManagerService immF = imm;
final RecognitionManagerService recognitionF = recognition;
final LocationManagerService locationF = location;
final CountryDetectorService countryDetectorF = countryDetector;
final NetworkTimeUpdateService networkTimeUpdaterF = networkTimeUpdater;
final CommonTimeManagementService commonTimeMgmtServiceF = commonTimeMgmtService;
final TextServicesManagerService textServiceManagerServiceF = tsms;
final StatusBarManagerService statusBarF = statusBar;
final DreamManagerService dreamyF = dreamy;
final AssetAtlasService atlasF = atlas;
final InputManagerService inputManagerF = inputManager;
final TelephonyRegistry telephonyRegistryF = telephonyRegistry;
final PrintManagerService printManagerF = printManager;

当服务就绪后,通知activity manager可以运行第三方的程序了,并且将各个服务的状态置为Ready。
例如:调用 mountServiceF.systemReady() ,但systemReady()函数中也做了一些各个Service自己的初始化工作。
可以跟着不同的模块再去整理。

ActivityManagerService.self().systemReady(new Runnable() {
public void run() {
Slog.i(TAG, "Making services ready");

try {
ActivityManagerService.self().startObservingNativeCrashes();
} catch (Throwable e) {
reportWtf("observing native crashes", e);
}
if (!headless) {
startSystemUi(contextF);
}
try {
if (mountServiceF != null) mountServiceF.systemReady();
} catch (Throwable e) {
reportWtf("making Mount Service ready", e);
}
try {
if (batteryF != null) batteryF.systemReady();
} catch (Throwable e) {
reportWtf("making Battery Service ready", e);
}
try {
if (networkManagementF != null) networkManagementF.systemReady();
} catch (Throwable e) {
reportWtf("making Network Managment Service ready", e);
}
.................................

.................................

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: