SystemServer进程分析

在进入SystemServer的main函数前,Zygote先会启动Binder线程池,用于与其他进程的通信。

public static final void zygoteInit(int targetSdkVersion, String[] argv,
            ClassLoader classLoader) throws Zygote.MethodAndArgsCaller {
        RuntimeInit.redirectLogStreams();
        RuntimeInit.commonInit();
     //调用native层代码,启动Binder线程池
        ZygoteInit.nativeZygoteInit();
        RuntimeInit.applicationInit(targetSdkVersion, argv, classLoader);
    }

RuntimeInit.applicationInit内部主要调用invokeStaticMain,该方法内部通过反射得到SystemerServer类,再反射调用其main方法。

private static void invokeStaticMain(String className, String[] argv, ClassLoader classLoader)
            throws Zygote.MethodAndArgsCaller {
        Class<?> cl;

        try {
            cl = Class.forName(className, true, classLoader);
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(
                    "Missing class when invoking static main " + className,
                    ex);
        }
        Method m;
        try {
            m = cl.getMethod("main", new Class[] { String[].class });
        } catch (NoSuchMethodException ex) {
            throw new RuntimeException(
                    "Missing static main on " + className, ex);
        } catch (SecurityException ex) {
            throw new RuntimeException(
                    "Problem getting static main on " + className, ex);
        }
        int modifiers = m.getModifiers();
        if (! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {
            throw new RuntimeException(
                    "Main method is not public and static on " + className);
        }
     //若抛出异常,则会调用 Zygote.MethodAndArgsCaller,这个方法最终也是通 
  过反射调用
     //SystemServer的main(),这里就不贴代码了,自己翻一下看看
        throw new Zygote.MethodAndArgsCaller(m, argv);
    }

再看SystemServer的man函数,主要调用了run()方法。

public static void main(String[] args) {
        new SystemServer().run();
    }
private void run() {
        try {
   
            VMRuntime.getRuntime().clearGrowthLimit();
            BaseBundle.setShouldDefuse(true);
            BinderInternal.disableBackgroundScheduling(true);
            BinerInternal.setMaxThreads(sMaxBinderThreads);
            // Prepare the main looper thread (this thread).
            android.os.Process.setThreadPriority(
                android.os.Process.THREAD_PRIORITY_FOREGROUND);
            android.os.Process.setCanSelfBackground(false);
              //创建消息Looper
            Looper.prepareMainLooper();
            // 加载native动态库
            System.loadLibrary("android_servers");
            performPendingShutdown();
            // 创建系统的上线
            createSystemContext();
            // 创建SystemServiceManager,用于对服务进行创建、启动和生命周期的管理
            mSystemServiceManager = new SystemServiceManager(mSystemContext);
            LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
        }

        // Start services.
        try {
            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartServices");
         //启动系统的引导服务
            startBootstrapServices();
         //启动系统的核心服务
            startCoreServices();
            //启动其他服务
            startOtherServices();
        } catch (Throwable ex) {
            Slog.e("System", "******************************************");
            Slog.e("System", "************ Failure starting system services", ex);
            throw ex;
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
        }
        Looper.loop();
        throw new RuntimeException("Main thread loop unexpectedly exited");
    }

简单列举几个三类服务

引导服务 作用
Install 系统安装APK的服务类
ActivityManagerServer Android四大组件的启动、切换、调度服务
PackageManagerServer 对APK解析、安装、卸载等操作
PowerManagerServer Power计算管理服务
......
核心服务 作用
DropBoxManagerServer 用于生成和管理系统运行时的一些日志文件
WebViewUpdateServer WebView更新服务
......
其他服务 作用
CameraServer 摄像头服务
AlarmManagerServer 全局定时管理服务
WindowManagerServer 窗口管理服务
NotificationManagerServer 通知管理服务
......

小结

1.启动Binder线程池,用于进程间通讯
2.创建SystemServerManager用于对各类服务的管理
3.启动三类系统服务

系统服务也已经启动,下面来看看应用启动过程Android应用程序启动分析

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容