android系统启动过程

来源:互联网 发布:java开发android应用 编辑:程序博客网 时间:2024/06/11 03:44

查找文件:  find  .  -iname "SystemServer.java"

比如:

appledeMacBook-Pro:frameworks apple$ find . -iname "SystemServer.java"./base/services/java/com/android/server/SystemServer.javaappledeMacBook-Pro:frameworks apple$ 


-----------------------------Android开机启动过程中需要查看文件:
ZygoteInit.java

appledeMacBook-Pro:frameworks apple$ find . -iname "ZygoteInit.java"./base/core/java/com/android/internal/os/ZygoteInit.java

在该类中几个重要的地方:

public static void main(String argv[]) {        try {            // Start profiling the zygote initialization.            SamplingProfilerIntegration.start();            boolean startSystemServer = false;            String socketName = "zygote";            String abiList = null;            for (int i = 1; i < argv.length; i++) {                if ("start-system-server".equals(argv[i])) {                    startSystemServer = true;                } else if (argv[i].startsWith(ABI_LIST_ARG)) {                    abiList = argv[i].substring(ABI_LIST_ARG.length());                } else if (argv[i].startsWith(SOCKET_NAME_ARG)) {                    socketName = argv[i].substring(SOCKET_NAME_ARG.length());                } else {                    throw new RuntimeException("Unknown command line argument: " + argv[i]);                }            }            if (abiList == null) {                throw new RuntimeException("No ABI list supplied.");            }            //这里注册流式socket,以便于fork新的dalvik进程。            registerZygoteSocket(socketName);            EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,                SystemClock.uptimeMillis());            preload();            EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,                SystemClock.uptimeMillis());            // Finish profiling the zygote initialization.            SamplingProfilerIntegration.writeZygoteSnapshot();            // Do an initial gc to clean up after startup            gc();            // Disable tracing so that forked processes do not inherit stale tracing tags from            // Zygote.            Trace.setTracingEnabled(false);              //这里创建SystemServer进程            if (startSystemServer) {                startSystemServer(abiList, socketName);            }            //这个方法是干啥??            //解释:通过Select方式监听端口,即异步读取消息,死循环,没有消息则一直阻塞在那里             Log.i(TAG, "Accepting command socket connections");            runSelectLoop(abiList);            closeServerSocket();        } catch (MethodAndArgsCaller caller) {            caller.run();        } catch (RuntimeException ex) {            Log.e(TAG, "Zygote died with exception", ex);            closeServerSocket();            throw ex;        }    }

这个方法是ZygoteInit的main方法。

preload(); 这个方法加载一些资源:

    static void preload() {        Log.d(TAG, "begin preload");        preloadClasses();        preloadResources();        preloadOpenGL();        preloadSharedLibraries();        // Ask the WebViewFactory to do any initialization that must run in the zygote process,        // for memory sharing purposes.        WebViewFactory.prepareWebViewInZygote();        Log.d(TAG, "end preload");    }

Zygote创建新的进程去启动系统服务。你可以在ZygoteInit类的”startSystemServer”方法中找到源代码。核心服务:启动电源管理器;创建Activity管理器;启动电话注册;启动包管理器;设置Activity管理服务为系统进程;启动上下文管理器;启动系统Context Providers;启动电池服务;启动定时管理器;启动传感服务;启动窗口管理器;启动蓝牙服务;启动挂载服务。其他服务:启动状态栏服务;启动硬件服务;启动网络状态服务;启动网络连接服务;启动通知管理器;启动设备存储监视服务;启动定位管理器;启动搜索服务;启动剪切板服务;启动登记服务;启动壁纸服务;启动音频服务;启动耳机监听;启动AdbSettingsObserver(处理adb命令)。


SystemServer.java
appledeMacBook-Pro:frameworks apple$ find . -iname "SystemServer.java"./base/services/java/com/android/server/SystemServer.java
        // Start services.        try {            startBootstrapServices();            startCoreServices();            startOtherServices();        } catch (Throwable ex) {            Slog.e("System", "******************************************");            Slog.e("System", "************ Failure starting system services", ex);            throw ex;        }


在startOtherServices()方法中:


// We now tell the activity manager it is okay to run third party        // code.  It will call back into us once it has gotten to the state        // where third party code can really run (but before it has actually        // started launching the initial applications), for us to complete our        // initialization.        mActivityManagerService.systemReady(new Runnable() {            @Override            public void run() {                Slog.i(TAG, "Making services ready");                mSystemServiceManager.startBootPhase(                        SystemService.PHASE_ACTIVITY_MANAGER_READY);                try {                    mActivityManagerService.startObservingNativeCrashes();                } catch (Throwable e) {                    reportWtf("observing native crashes", e);                } 。。。。。。


ActivityManagerService.java
appledeMacBook-Pro:frameworks apple$ find . -iname "ActivityManagerService.java"./base/services/core/java/com/android/server/am/ActivityManagerService.java

    public void systemReady(final Runnable goingCallback) {        synchronized(this) {            if (mSystemReady) {                // If we're done calling all the receivers, run the next "boot phase" passed in                // by the SystemServer                if (goingCallback != null) {                    goingCallback.run();                }                return;            } ......

。。。。。。

参考博客

点击打开链接

0 0