1、C/S
framework层关键的代码可见此链接
服务端关键类
frameworks/base/services/java/com/android/server/
- SystemServer.java -- vm通过main方法启动服务端
frameworks/base/services/core/java/com/android/server/
- SystemServiceManager.java -- 全部系统服务的管理类
- ServiceThread.java
- pm/Installer.java
- am/ActivityManagerService.java -- AMS关键服务类
客户端关键类
frameworks/base/core/java/android/app/
- ActivityThread.java -- vm通过main方法启动客户端
- LoadedApk.java
- ContextImpl.java
2、S启动过程
2.1、SystemServer#main
public static void main(String[] args) {
new SystemServer().run();
}
2.2、SystemServer#run
// Initialize the system context.
createSystemContext(); 这里通过ActivityThread.systemMain()创建ActivityThread获取Context
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
mSystemServiceManager.setRuntimeRestarted(mRuntimeRestart);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
。。。。
startBootstrapServices();
startCoreServices();
startOtherServices();
。。。。
SystemServer#startBootstrapServices
启动启动系统必要的关键service
private void startBootstrapServices() {
...
Installer installer = mSystemServiceManager.startService(Installer.class);
...
mSystemServiceManager.startService(DeviceIdentifiersPolicyService.class);
...
//通过mSystemServiceManager反射创建LifeCycle,从而创建AMS
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
//相当于 Lifecycle.getService
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
.....
mActivityManagerService.setSystemProcess(); //设置系统进程
...
}
SystemServiceManager.startService(ActivityManagerService.Lifecycle.class) 功能主要:
创建ActivityManagerService.Lifecycle对象,从而创建AMS
调用Lifecycle.onStart()方法
ActivityManagerService#Lifecycle构造方法
public static final class Lifecycle extends SystemService {
private final ActivityManagerService mService;
public Lifecycle(Context context) {
super(context);
//内部创建AMS
mService = new ActivityManagerService(context);
}
@Override
public void onStart() {
mService.start();
}
AMS创建
public ActivityManagerService(Context systemContext) {
mContext = systemContext;
mFactoryTest = FactoryTest.getMode();//默认为FACTORY_TEST_OFF
mSystemThread = ActivityThread.currentActivityThread();
//1、创建名为"ActivityManager"的前台线程,也就是UI线程,只许system设置,并获取mHandler
mHandlerThread = new ServiceThread(TAG, android.os.Process.THREAD_PRIORITY_FOREGROUND, false);
mHandlerThread.start();
mHandler = new MainHandler(mHandlerThread.getLooper());
//2、通过UiThread类,创建名为"android.ui"的线程
mUiHandler = new UiHandler();
//前台广播接收器,在运行超过10s将放弃执行
mFgBroadcastQueue = new BroadcastQueue(this, mHandler,
"foreground", BROADCAST_FG_TIMEOUT, false);
//后台广播接收器,在运行超过60s将放弃执行
mBgBroadcastQueue = new BroadcastQueue(this, mHandler,
"background", BROADCAST_BG_TIMEOUT, true);
mBroadcastQueues[0] = mFgBroadcastQueue;
mBroadcastQueues[1] = mBgBroadcastQueue;
//创建ActiveServices,其中非低内存手机mMaxStartingBackground为8
mServices = new ActiveServices(this);
mProviderMap = new ProviderMap(this);
//创建目录/data/system
File dataDir = Environment.getDataDirectory();
File systemDir = new File(dataDir, "system");
systemDir.mkdirs();
//创建服务BatteryStatsService
mBatteryStatsService = new BatteryStatsService(systemDir, mHandler);
mBatteryStatsService.getActiveStatistics().readLocked();
...
//创建进程统计服务,信息保存在目录/data/system/procstats,
mProcessStats = new ProcessStatsService(this, new File(systemDir, "procstats"));
mAppOpsService = new AppOpsService(new File(systemDir, "appops.xml"), mHandler);
mGrantFile = new AtomicFile(new File(systemDir, "urigrants.xml"));
// User 0是第一个,也是唯一的一个开机过程中运行的用户
mStartedUsers.put(UserHandle.USER_OWNER, new UserState(UserHandle.OWNER, true));
mUserLru.add(UserHandle.USER_OWNER);
updateStartedUserArrayLocked();
...
//CPU使用情况的追踪器执行初始化
mProcessCpuTracker.init();
...
mRecentTasks = new RecentTasks(this);
// 创建ActivityStackSupervisor对象
mStackSupervisor = new ActivityStackSupervisor(this, mRecentTasks);
mTaskPersister = new TaskPersister(systemDir, mStackSupervisor, mRecentTasks);
//3、创建名为"CpuTracker"的线程
mProcessCpuThread = new Thread("CpuTracker")
....
�
�该过程共创建了3个线程,分别为“ActivityManager”,“android.ui”,“CpuTracker”。
AMS.start
private void start() {
Process.removeAllProcessGroups(); //移除所有的进程组
mProcessCpuThread.start(); //启动CpuTracker线程
mBatteryStatsService.publish(mContext); //启动电池统计服务
mAppOpsService.publish(mContext);
//创建LocalService,并添加到LocalServices
LocalServices.addService(ActivityManagerInternal.class, new LocalService());
}
LocalService是继承至ActivityManagerInternal(Activity manager local system service interface)
AMS#setSystemProcess
此方法是在SystemServer#startBootstrapServices中触发调用的,主要是各种服务的注册
public void setSystemProcess() {
try {
ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);//注册自己
ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
ServiceManager.addService("meminfo", new MemBinder(this));
ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
ServiceManager.addService("dbinfo", new DbBinder(this));
....
mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
}
mSystemThread#installSystemApplicationInfo调用链:设置包名为”android”的应用信息
mSystemThread#installSystemApplicationInfo
ContextImpl#installSystemApplicationInfo
LoadedApk#installSystemApplicationInfo
/**
* Sets application info about the system package.
*/
void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
assert info.packageName.equals("android");
mApplicationInfo = info;
mClassLoader = classLoader;
}