系统在启动的时候会启动一个叫做PackageManagerService
的服务,顾名思义,这个服务主要管理安装在设备上的应用程序,其中最为重要的工作就是在在系统启动之后,PackageManagerService
会扫描特定目录下地以apk为后缀的文件,然后将对应的应用安装到系统中。注意,这里的安装并不是我们平时所说的安装,它指的的是将存放在磁盘之上的静态应用程序文件进行解析,并将相关信息注册到系统中。而具体的解析工作实际就是读取应用的配置文件manifest.xml
,并将文件中配置的组件
(Activity
,Service
,BroadcastRecevier
,ContentProvider
),权限等信息注册到PackageManagerService
中。
本篇博客主要介绍PackageManagerService
的启动过程,以及PackageManagerService
如何安装各个应用程序。
1.PackageManagerService
启动过程
和ActivityManagerService
,WindowManagerService
一样,PackageManagerService
是一个系统级的服务,运行在独立的进程中,而所有的系统级服务都是由SystemServer
启动的。所以首先来看看SystemServer
的启动过程。
1) SystemServer
启动:
SystemServer组件是由Zygote进程负责启动的,启动的时候就会调用它的main函数,这个函数主要调用了JNI方法init1来做一些系统初始化的工作。
public class SystemServer
{
......
native public static void init1(String[] args);
......
public static void main(String[] args) {
......
init1(args);
......
}
......
}
2)SystemServer.system_init
经过一系列调用后转到system_init
方法,这是一个JNI方法
extern "C" status_t system_init()
{
LOGI("Entered system_init()");
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager();
LOGI("ServiceManager: %p\n", sm.get());
sp<GrimReaper> grim = new GrimReaper();
sm->asBinder()->linkToDeath(grim, grim.get(), 0);
char propBuf[PROPERTY_VALUE_MAX];
property_get("system_init.startsurfaceflinger", propBuf, "1");
if (strcmp(propBuf, "1") == 0) {
// Start the SurfaceFlinger
SurfaceFlinger::instantiate();
}
// Start the sensor service
SensorService::instantiate();
// On the simulator, audioflinger et al don't get started the
// same way as on the device, and we need to start them here
if (!proc->supportsProcesses()) {
// Start the AudioFlinger
AudioFlinger::instantiate();
// Start the media playback service
MediaPlayerService::instantiate();
// Start the camera service
CameraService::instantiate();
// Start the audio policy service
AudioPolicyService::instantiate();
}
// And now start the Android runtime. We have to do this bit
// of nastiness because the Android runtime initialization requires
// some of the core system services to already be started.
// All other servers should just start the Android runtime at
// the beginning of their processes's main(), before calling
// the init function.
LOGI("System server: starting Android runtime.\n");
AndroidRuntime* runtime = AndroidRuntime::getRuntime();
LOGI("System server: starting Android services.\n");
runtime->callStatic("com/android/server/SystemServer", "init2");
// If running in our own process, just go into the thread
// pool. Otherwise, call the initialization finished
// func to let this process continue its initilization.
if (proc->supportsProcesses()) {
LOGI("System server: entering thread pool.\n");
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
LOGI("System server: exiting thread pool.\n");
}
return NO_ERROR;
}
在这个方法中,创建了SurfaceFlinger、SensorService、AudioFlinger、MediaPlayerService、CameraService和AudioPolicyService这几个服务,然后就通过系统全局唯一的AndroidRuntime实例变量runtime的callStatic来调用SystemServer的init2函数了。init2函数很简单,创建一个线程,而PackageManagerService
就是在这个线程中创建的。
public class SystemServer
{
......
public static final void init2() {
Slog.i(TAG, "Entered the Android system server!");
Thread thr = new ServerThread();
thr.setName("android.server.ServerThread");
thr.start();
}
}