美文网首页
Android之SystemServer介绍(二)

Android之SystemServer介绍(二)

作者: Lee_5566 | 来源:发表于2021-03-11 20:15 被阅读0次
image.png

目录

Android之zygote源码剖析(一)
Android之zygote源码剖析(二)
Android之zygote源码剖析(三)
Android之SystemServer介绍(一)
Android之SystemServer介绍(二)
Android之Launcher介绍(一)
Android之Launcher介绍(二)

SystemServer启动

上文讲到执行到SystemServer类的main函数。MethodAndArgsCaller函数在Zygote.java类中:

    public static class MethodAndArgsCaller extends Exception
            implements Runnable {
        /** method to call */
        private final Method mMethod;

        /** argument array */
        private final String[] mArgs;

        public MethodAndArgsCaller(Method method, String[] args) {
            mMethod = method;
            mArgs = args;
        }

        public void run() {
            try {
                mMethod.invoke(null, new Object[] { mArgs });
            } catch (IllegalAccessException ex) {
                throw new RuntimeException(ex);
            } catch (InvocationTargetException ex) {
                Throwable cause = ex.getCause();
                if (cause instanceof RuntimeException) {
                    throw (RuntimeException) cause;
                } else if (cause instanceof Error) {
                    throw (Error) cause;
                }
                throw new RuntimeException(ex);
            }
        }
    }

可以看到只是简单的执行了main函数。

下面进入SystemServer.java的main函数:

    /**
     * The main entry point from zygote.
     */
    public static void main(String[] args) {
        new SystemServer().run();
    }

执行过来后,就是直接执行了run函数。

run函数代码比较多:

    private void run() {
        try {
            ……

            // 设置一些属性信息
            String timezoneProperty =  SystemProperties.get("persist.sys.timezone");
            if (timezoneProperty == null || timezoneProperty.isEmpty()) {
                Slog.w(TAG, "Timezone not set; setting to GMT.");
                SystemProperties.set("persist.sys.timezone", "GMT");
            }

            if (!SystemProperties.get("persist.sys.language").isEmpty()) {
                final String languageTag = Locale.getDefault().toLanguageTag();

                SystemProperties.set("persist.sys.locale", languageTag);
                SystemProperties.set("persist.sys.language", "");
                SystemProperties.set("persist.sys.country", "");
                SystemProperties.set("persist.sys.localevar", "");
            }
            ……
            // 设置消息looper
            Looper.prepareMainLooper();

            // 加载android_servers动态库
            System.loadLibrary("android_servers");
            performPendingShutdown();

            // 创建系统的context
            createSystemContext();

            // 创建SystemServiceManager服务,后续其对系统服务进行创建、启动和管理
            mSystemServiceManager = new SystemServiceManager(mSystemContext);
            // 设置SystemServiceManager的runtime
            mSystemServiceManager.setRuntimeRestarted(mRuntimeRestart);

            // 添加SystemServiceManager服务进入LocalServices队列
            LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
            SystemServerInitThreadPool.get();
        } finally {
            traceEnd();
        }

        try {
            traceBeginAndSlog("StartServices");
            // 启动引导服务
            startBootstrapServices();
            // 启动核心服务
            startCoreServices();
            // 启动其他服务
            startOtherServices();
            SystemServerInitThreadPool.shutdown();
        } catch (Throwable ex) {
            Slog.e("System", "******************************************");
            Slog.e("System", "************ Failure starting system services", ex);
            throw ex;
        } finally {
            traceEnd();
        }
        ……
    }

代码中添加了注释说明。

SystemServiceManager

继续来看下SystemServiceManager类, SystemServiceManager的对象声明也在SystemService类中:

    private SystemServiceManager mSystemServiceManager;

我们主要看下他如何启动服务的:

    public void startService(@NonNull final SystemService service) {
        // Register it.
        mServices.add(service);
        // Start it.
        long time = System.currentTimeMillis();
        try {
            service.onStart();
        } catch (RuntimeException ex) {
            throw new RuntimeException("Failed to start service " + service.getClass().getName()
                    + ": onStart threw an exception", ex);
        }
        warnIfTooLong(System.currentTimeMillis() - time, service, "onStart");
    }

执行startServer之后,将服务添加入mServices队列中。

    private final ArrayList<SystemService> mServices = new ArrayList<SystemService>();

并执行服务的onStart函数,这样就启动服务了(线程)。

image.png

相关文章

网友评论

      本文标题:Android之SystemServer介绍(二)

      本文链接:https://www.haomeiwen.com/subject/cgsnqltx.html