国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

App啟動(dòng)流程

geekidentity / 897人閱讀

摘要:孵化進(jìn)程相當(dāng)于是系統(tǒng)的根進(jìn)程,后面所有的進(jìn)程都是通過這個(gè)進(jìn)程出來的雖然進(jìn)程相當(dāng)于系統(tǒng)的根進(jìn)程,但是事實(shí)上它也是由系統(tǒng)的進(jìn)程啟動(dòng)的。

目錄介紹

1.什么是Zygote進(jìn)程

1.1 簡(jiǎn)單介紹

1.2 各個(gè)進(jìn)程的先后順序

1.3 進(jìn)程作用說明

2.Zygote進(jìn)程的啟動(dòng)流程

2.1 源碼位置

2.2 ZygoteInit類的main方法

2.3 registerZygoteSocket(socketName)分析

2.4 preLoad()方法分析

2.5 startSystemServer()啟動(dòng)進(jìn)程

3.SystemServer進(jìn)程啟動(dòng)流程

3.1 SystemServer進(jìn)程簡(jiǎn)介

3.2 SystemServer的main方法

3.3 查看run方法

3.4 run方法中createSystemContext()解析

3.5 mSystemServiceManager的創(chuàng)建

4.啟動(dòng)服務(wù)

4.1 啟動(dòng)哪些服務(wù)

4.2 啟動(dòng)服務(wù)流程源碼分析

4.3 啟動(dòng)部分服務(wù)

好消息

博客筆記大匯總【16年3月到至今】,包括Java基礎(chǔ)及深入知識(shí)點(diǎn),Android技術(shù)博客,Python學(xué)習(xí)筆記等等,還包括平時(shí)開發(fā)中遇到的bug匯總,當(dāng)然也在工作之余收集了大量的面試題,長(zhǎng)期更新維護(hù)并且修正,持續(xù)完善……開源的文件是markdown格式的!同時(shí)也開源了生活博客,從12年起,積累共計(jì)47篇[近20萬(wàn)字],轉(zhuǎn)載請(qǐng)注明出處,謝謝!

鏈接地址:https://github.com/yangchong2...

如果覺得好,可以star一下,謝謝!當(dāng)然也歡迎提出建議,萬(wàn)事起于忽微,量變引起質(zhì)變!

1.什么是Zygote進(jìn)程 1.1 簡(jiǎn)單介紹

Zygote進(jìn)程是所有的android進(jìn)程的父進(jìn)程,包括SystemServer和各種應(yīng)用進(jìn)程都是通過Zygote進(jìn)程fork出來的。Zygote(孵化)進(jìn)程相當(dāng)于是android系統(tǒng)的根進(jìn)程,后面所有的進(jìn)程都是通過這個(gè)進(jìn)程fork出來的

雖然Zygote進(jìn)程相當(dāng)于Android系統(tǒng)的根進(jìn)程,但是事實(shí)上它也是由Linux系統(tǒng)的init進(jìn)程啟動(dòng)的。

1.2 各個(gè)進(jìn)程的先后順序

init進(jìn)程 --> Zygote進(jìn)程 --> SystemServer進(jìn)程 -->各種應(yīng)用進(jìn)程

1.3 進(jìn)程作用說明

init進(jìn)程:linux的根進(jìn)程,android系統(tǒng)是基于linux系統(tǒng)的,因此可以算作是整個(gè)android操作系統(tǒng)的第一個(gè)進(jìn)程;

Zygote進(jìn)程:android系統(tǒng)的根進(jìn)程,主要作用:可以作用Zygote進(jìn)程fork出SystemServer進(jìn)程和各種應(yīng)用進(jìn)程;

SystemService進(jìn)程:主要是在這個(gè)進(jìn)程中啟動(dòng)系統(tǒng)的各項(xiàng)服務(wù),比如ActivityManagerService,PackageManagerService,WindowManagerService服務(wù)等等;

各種應(yīng)用進(jìn)程:?jiǎn)?dòng)自己編寫的客戶端應(yīng)用時(shí),一般都是重新啟動(dòng)一個(gè)應(yīng)用進(jìn)程,有自己的虛擬機(jī)與運(yùn)行環(huán)境;

2.Zygote進(jìn)程的啟動(dòng)流程 2.1 源碼位置

位置:frameworks/base/core/java/com/android/internal/os/ZygoteInit.java

Zygote進(jìn)程mian方法主要執(zhí)行邏輯:

初始化DDMS;

注冊(cè)Zygote進(jìn)程的socket通訊;

初始化Zygote中的各種類,資源文件,OpenGL,類庫(kù),Text資源等等;

初始化完成之后fork出SystemServer進(jìn)程;

fork出SystemServer進(jìn)程之后,關(guān)閉socket連接;

2.2 ZygoteInit類的main方法

init進(jìn)程在啟動(dòng)Zygote進(jìn)程時(shí)一般都會(huì)調(diào)用ZygoteInit類的main方法,因此這里看一下該方法的具體實(shí)現(xiàn)(基于android23源碼);

調(diào)用enableDdms(),設(shè)置DDMS可用,可以發(fā)現(xiàn)DDMS啟動(dòng)的時(shí)機(jī)還是比較早的,在整個(gè)Zygote進(jìn)程剛剛開始要啟動(dòng)額時(shí)候就設(shè)置可用。

之后初始化各種參數(shù)

通過調(diào)用registerZygoteSocket方法,注冊(cè)為Zygote進(jìn)程注冊(cè)Socket

然后調(diào)用preload方法實(shí)現(xiàn)預(yù)加載各種資源

然后通過調(diào)用startSystemServer開啟SystemServer服務(wù),這個(gè)是重點(diǎn)

public static void main(String argv[]) {
    try {
        //設(shè)置ddms可以用
        RuntimeInit.enableDdms();
        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.");
        }

        registerZygoteSocket(socketName);
        EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,
            SystemClock.uptimeMillis());
        preload();
        EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
            SystemClock.uptimeMillis());
        SamplingProfilerIntegration.writeZygoteSnapshot();

        gcAndFinalize();
        Trace.setTracingEnabled(false);

        if (startSystemServer) {
            startSystemServer(abiList, socketName);
        }

        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;
    }
}

2.3 registerZygoteSocket(socketName)分析

調(diào)用registerZygoteSocket(String socketName)為Zygote進(jìn)程注冊(cè)socket

private static void registerZygoteSocket(String socketName) {
    if (sServerSocket == null) {
        int fileDesc;
        final String fullSocketName = ANDROID_SOCKET_PREFIX + socketName;
        try {
            String env = System.getenv(fullSocketName);
            fileDesc = Integer.parseInt(env);
        } catch (RuntimeException ex) {
            throw new RuntimeException(fullSocketName + " unset or invalid", ex);
        }

        try {
            FileDescriptor fd = new FileDescriptor();
            fd.setInt$(fileDesc);
            sServerSocket = new LocalServerSocket(fd);
        } catch (IOException ex) {
            throw new RuntimeException(
                    "Error binding to local socket "" + fileDesc + """, ex);
        }
    }
}

2.4 preLoad()方法分析

源碼如下所示

static void preload() {
    Log.d(TAG, "begin preload");
    preloadClasses();
    preloadResources();
    preloadOpenGL();
    preloadSharedLibraries();
    preloadTextResources();
    // 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");
}

大概操作是這樣的:

preloadClasses()用于初始化Zygote中需要的class類;

preloadResources()用于初始化系統(tǒng)資源;

preloadOpenGL()用于初始化OpenGL;

preloadSharedLibraries()用于初始化系統(tǒng)libraries;

preloadTextResources()用于初始化文字資源;

prepareWebViewInZygote()用于初始化webview;

2.5 startSystemServer()啟動(dòng)進(jìn)程

這段邏輯的執(zhí)行邏輯就是通過Zygote fork出SystemServer進(jìn)程

private static boolean startSystemServer(String abiList, String socketName)
        throws MethodAndArgsCaller, RuntimeException {
    long capabilities = posixCapabilitiesAsBits(
        OsConstants.CAP_BLOCK_SUSPEND,
        OsConstants.CAP_KILL,
        OsConstants.CAP_NET_ADMIN,
        OsConstants.CAP_NET_BIND_SERVICE,
        OsConstants.CAP_NET_BROADCAST,
        OsConstants.CAP_NET_RAW,
        OsConstants.CAP_SYS_MODULE,
        OsConstants.CAP_SYS_NICE,
        OsConstants.CAP_SYS_RESOURCE,
        OsConstants.CAP_SYS_TIME,
        OsConstants.CAP_SYS_TTY_CONFIG
    );
    /* Hardcoded command line to start the system server */
    String args[] = {
        "--setuid=1000",
        "--setgid=1000",
        "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1021,1032,3001,3002,3003,3006,3007",
        "--capabilities=" + capabilities + "," + capabilities,
        "--nice-name=system_server",
        "--runtime-args",
        "com.android.server.SystemServer",
    };
    ZygoteConnection.Arguments parsedArgs = null;

    int pid;

    try {
        parsedArgs = new ZygoteConnection.Arguments(args);
        ZygoteConnection.applyDebuggerSystemProperty(parsedArgs);
        ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs);

        /* Request to fork the system server process */
        pid = Zygote.forkSystemServer(
                parsedArgs.uid, parsedArgs.gid,
                parsedArgs.gids,
                parsedArgs.debugFlags,
                null,
                parsedArgs.permittedCapabilities,
                parsedArgs.effectiveCapabilities);
    } catch (IllegalArgumentException ex) {
        throw new RuntimeException(ex);
    }

    /* For child process */
    if (pid == 0) {
        if (hasSecondZygote(abiList)) {
            waitForSecondaryZygote(socketName);
        }

        handleSystemServerProcess(parsedArgs);
    }

    return true;
}

3.SystemServer進(jìn)程啟動(dòng)流程 3.1 SystemServer進(jìn)程簡(jiǎn)介

SystemServer進(jìn)程主要的作用是在這個(gè)進(jìn)程中啟動(dòng)各種系統(tǒng)服務(wù),比如ActivityManagerService,PackageManagerService,WindowManagerService服務(wù),以及各種系統(tǒng)性的服務(wù)其實(shí)都是在SystemServer進(jìn)程中啟動(dòng)的,而當(dāng)我們的應(yīng)用需要使用各種系統(tǒng)服務(wù)的時(shí)候其實(shí)也是通過與SystemServer進(jìn)程通訊獲取各種服務(wù)對(duì)象的句柄的。

3.2 SystemServer的main方法

如下所示,比較簡(jiǎn)單,只是new出一個(gè)SystemServer對(duì)象并執(zhí)行其run方法,查看SystemServer類的定義我們知道其實(shí)final類型的,所以我們一般不能重寫或者繼承。

3.3 查看run方法

代碼如下所示

首先判斷系統(tǒng)當(dāng)前時(shí)間,若當(dāng)前時(shí)間小于1970年1月1日,則一些初始化操作可能會(huì)處所,所以當(dāng)系統(tǒng)的當(dāng)前時(shí)間小于1970年1月1日的時(shí)候,設(shè)置系統(tǒng)當(dāng)前時(shí)間為該時(shí)間點(diǎn)。

然后是設(shè)置系統(tǒng)的語(yǔ)言環(huán)境等

接著設(shè)置虛擬機(jī)運(yùn)行內(nèi)存,加載運(yùn)行庫(kù),設(shè)置SystemServer的異步消息

private void run() {
    if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
        Slog.w(TAG, "System clock is before 1970; setting to 1970.");
        SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
    }

    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", "");
    }

    Slog.i(TAG, "Entered the Android system server!");
    EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, SystemClock.uptimeMillis());

    SystemProperties.set("persist.sys.dalvik.vm.lib.2", VMRuntime.getRuntime().vmLibrary());

    if (SamplingProfilerIntegration.isEnabled()) {
        SamplingProfilerIntegration.start();
        mProfilerSnapshotTimer = new Timer();
        mProfilerSnapshotTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                SamplingProfilerIntegration.writeSnapshot("system_server", null);
            }
        }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
    }

    // Mmmmmm... more memory!
    VMRuntime.getRuntime().clearGrowthLimit();

    // The system server has to run all of the time, so it needs to be
    // as efficient as possible with its memory usage.
    VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);

    // Some devices rely on runtime fingerprint generation, so make sure
    // we"ve defined it before booting further.
    Build.ensureFingerprintProperty();

    // Within the system server, it is an error to access Environment paths without
    // explicitly specifying a user.
    Environment.setUserRequired(true);

    // Ensure binder calls into the system always run at foreground priority.
    BinderInternal.disableBackgroundScheduling(true);

    // Prepare the main looper thread (this thread).
    android.os.Process.setThreadPriority(
            android.os.Process.THREAD_PRIORITY_FOREGROUND);
    android.os.Process.setCanSelfBackground(false);
    Looper.prepareMainLooper();

    // Initialize native services.
    System.loadLibrary("android_servers");

    // Check whether we failed to shut down last time we tried.
    // This call may not return.
    performPendingShutdown();

    // Initialize the system context.
    createSystemContext();

    // Create the system service manager.
    mSystemServiceManager = new SystemServiceManager(mSystemContext);
    LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);

    // Start services.
    try {
        startBootstrapServices();
        startCoreServices();
        startOtherServices();
    } catch (Throwable ex) {
        Slog.e("System", "******************************************");
        Slog.e("System", "************ Failure starting system services", ex);
        throw ex;
    }

    // For debug builds, log event loop stalls to dropbox for analysis.
    if (StrictMode.conditionallyEnableDebugLogging()) {
        Slog.i(TAG, "Enabled StrictMode for system server main thread.");
    }

    // Loop forever.
    Looper.loop();
    throw new RuntimeException("Main thread loop unexpectedly exited");
}

然后下面的代碼是:

// Initialize the system context.
createSystemContext();

// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);

// Start services.
try {
    startBootstrapServices();
    startCoreServices();
    startOtherServices();
} catch (Throwable ex) {
    Slog.e("System", "******************************************");
    Slog.e("System", "************ Failure starting system services", ex);
    throw ex;
}

3.4 run方法中createSystemContext()解析

調(diào)用createSystemContext()方法:

可以看到在SystemServer進(jìn)程中也存在著Context對(duì)象,并且是通過ActivityThread.systemMain方法創(chuàng)建context的,這一部分的邏輯以后會(huì)通過介紹Activity的啟動(dòng)流程來介紹,這里就不在擴(kuò)展,只知道在SystemServer進(jìn)程中也需要?jiǎng)?chuàng)建Context對(duì)象。

private void createSystemContext() {
    ActivityThread activityThread = ActivityThread.systemMain();
    mSystemContext = activityThread.getSystemContext();
    mSystemContext.setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar);
}

3.5 mSystemServiceManager的創(chuàng)建

看run方法中,通過SystemServiceManager的構(gòu)造方法創(chuàng)建了一個(gè)新的SystemServiceManager對(duì)象,我們知道SystemServer進(jìn)程主要是用來構(gòu)建系統(tǒng)各種service服務(wù)的,而SystemServiceManager就是這些服務(wù)的管理對(duì)象。

然后調(diào)用:

將SystemServiceManager對(duì)象保存SystemServer進(jìn)程中的一個(gè)數(shù)據(jù)結(jié)構(gòu)中。

LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);

最后開始執(zhí)行:

// Start services.
try {
    startBootstrapServices();
    startCoreServices();
    startOtherServices();
} catch (Throwable ex) {
    Slog.e("System", "******************************************");
    Slog.e("System", "************ Failure starting system services", ex);
    throw ex;
}

里面主要涉及了是三個(gè)方法:

startBootstrapServices() 主要用于啟動(dòng)系統(tǒng)Boot級(jí)服務(wù)

startCoreServices() 主要用于啟動(dòng)系統(tǒng)核心的服務(wù)

startOtherServices() 主要用于啟動(dòng)一些非緊要或者是非需要及時(shí)啟動(dòng)的服務(wù)

4.啟動(dòng)服務(wù) 4.1 啟動(dòng)哪些服務(wù)

在開始執(zhí)行啟動(dòng)服務(wù)之前總是會(huì)先嘗試通過socket方式連接Zygote進(jìn)程,在成功連接之后才會(huì)開始啟動(dòng)其他服務(wù)。

4.2 啟動(dòng)服務(wù)流程源碼分析

首先看一下startBootstrapServices方法:

private void startBootstrapServices() {
    Installer installer = mSystemServiceManager.startService(Installer.class);

    mActivityManagerService = mSystemServiceManager.startService(
            ActivityManagerService.Lifecycle.class).getService();
    mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
    mActivityManagerService.setInstaller(installer);

    mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);

    mActivityManagerService.initPowerManagement();

    // Manages LEDs and display backlight so we need it to bring up the display.
    mSystemServiceManager.startService(LightsService.class);

    // Display manager is needed to provide display metrics before package manager
    // starts up.
    mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);

    // We need the default display before we can initialize the package manager.
    mSystemServiceManager.startBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY);

    // Only run "core" apps if we"re encrypting the device.
    String cryptState = SystemProperties.get("vold.decrypt");
    if (ENCRYPTING_STATE.equals(cryptState)) {
        Slog.w(TAG, "Detected encryption in progress - only parsing core apps");
        mOnlyCore = true;
    } else if (ENCRYPTED_STATE.equals(cryptState)) {
        Slog.w(TAG, "Device encrypted - only parsing core apps");
        mOnlyCore = true;
    }

    // Start the package manager.
    Slog.i(TAG, "Package Manager");
    mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
            mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
    mFirstBoot = mPackageManagerService.isFirstBoot();
    mPackageManager = mSystemContext.getPackageManager();

    Slog.i(TAG, "User Service");
    ServiceManager.addService(Context.USER_SERVICE, UserManagerService.getInstance());

    // Initialize attribute cache used to cache resources from packages.
    AttributeCache.init(mSystemContext);

    // Set up the Application instance for the system process and get started.
    mActivityManagerService.setSystemProcess();

    // The sensor service needs access to package manager service, app ops
    // service, and permissions service, therefore we start it after them.
    startSensorService();
}

先執(zhí)行:

Installer installer = mSystemServiceManager.startService(Installer.class);

mSystemServiceManager是系統(tǒng)服務(wù)管理對(duì)象,在main方法中已經(jīng)創(chuàng)建完成,這里我們看一下其startService方法的具體實(shí)現(xiàn):

可以看到通過反射器構(gòu)造方法創(chuàng)建出服務(wù)類,然后添加到SystemServiceManager的服務(wù)列表數(shù)據(jù)中,最后調(diào)用了service.onStart()方法,因?yàn)閭鬟f的是Installer.class

public  T startService(Class serviceClass) {
    final String name = serviceClass.getName();
    Slog.i(TAG, "Starting " + name);

    // Create the service.
    if (!SystemService.class.isAssignableFrom(serviceClass)) {
        throw new RuntimeException("Failed to create " + name
                + ": service must extend " + SystemService.class.getName());
    }
    final T service;
    try {
        Constructor constructor = serviceClass.getConstructor(Context.class);
        service = constructor.newInstance(mContext);
    } catch (InstantiationException ex) {
        throw new RuntimeException("Failed to create service " + name
                + ": service could not be instantiated", ex);
    } catch (IllegalAccessException ex) {
        throw new RuntimeException("Failed to create service " + name
                + ": service must have a public constructor with a Context argument", ex);
    } catch (NoSuchMethodException ex) {
        throw new RuntimeException("Failed to create service " + name
                + ": service must have a public constructor with a Context argument", ex);
    } catch (InvocationTargetException ex) {
        throw new RuntimeException("Failed to create service " + name
                + ": service constructor threw an exception", ex);
    }

    // Register it.
    mServices.add(service);

    // Start it.
    try {
        service.onStart();
    } catch (RuntimeException ex) {
        throw new RuntimeException("Failed to start service " + name
                + ": onStart threw an exception", ex);
    }
    return service;
}

看一下Installer的onStart方法:

很簡(jiǎn)單就是執(zhí)行了mInstaller的waitForConnection方法,這里簡(jiǎn)單介紹一下Installer類,該類是系統(tǒng)安裝apk時(shí)的一個(gè)服務(wù)類,繼承SystemService(系統(tǒng)服務(wù)的一個(gè)抽象接口),需要在啟動(dòng)完成Installer服務(wù)之后才能啟動(dòng)其他的系統(tǒng)服務(wù)。

@Override
public void onStart() {
    Slog.i(TAG, "Waiting for installd to be ready.");
    mInstaller.waitForConnection();
}

然后查看waitForConnection()方法:

通過追蹤代碼可以發(fā)現(xiàn),其在不斷的通過ping命令連接Zygote進(jìn)程(SystemServer和Zygote進(jìn)程通過socket方式通訊,其他進(jìn)程通過Binder方式通訊)

public void waitForConnection() {
    for (;;) {
        if (execute("ping") >= 0) {
            return;
        }
        Slog.w(TAG, "installd not ready");
        SystemClock.sleep(1000);
    }
}

繼續(xù)看startBootstrapServices方法:

這段代碼主要是用于啟動(dòng)ActivityManagerService服務(wù),并為其設(shè)置SysServiceManager和Installer。ActivityManagerService是系統(tǒng)中一個(gè)非常重要的服務(wù),Activity,service,Broadcast,contentProvider都需要通過其余系統(tǒng)交互。

// Activity manager runs the show.
mActivityManagerService = mSystemServiceManager.startService(
        ActivityManagerService.Lifecycle.class).getService();
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);

首先看一下Lifecycle類的定義:

可以看到其實(shí)ActivityManagerService的一個(gè)靜態(tài)內(nèi)部類,在其構(gòu)造方法中會(huì)創(chuàng)建一個(gè)ActivityManagerService,通過剛剛對(duì)Installer服務(wù)的分析我們知道,SystemServiceManager的startService方法會(huì)調(diào)用服務(wù)的onStart()方法,而在Lifecycle類的定義中我們看到其onStart()方法直接調(diào)用了mService.start()方法,mService是Lifecycle類中對(duì)ActivityManagerService的引用

public static final class Lifecycle extends SystemService {
    private final ActivityManagerService mService;

    public Lifecycle(Context context) {
        super(context);
        mService = new ActivityManagerService(context);
    }

    @Override
    public void onStart() {
        mService.start();
    }

    public ActivityManagerService getService() {
        return mService;
    }
}

4.3 啟動(dòng)部分服務(wù)

啟動(dòng)PowerManagerService服務(wù):

啟動(dòng)方式跟上面的ActivityManagerService服務(wù)相似都會(huì)調(diào)用其構(gòu)造方法和onStart方法,PowerManagerService主要用于計(jì)算系統(tǒng)中和Power相關(guān)的計(jì)算,然后決策系統(tǒng)應(yīng)該如何反應(yīng)。同時(shí)協(xié)調(diào)Power如何與系統(tǒng)其它模塊的交互,比如沒有用戶活動(dòng)時(shí),屏幕變暗等等。

mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);

然后是啟動(dòng)LightsService服務(wù)

主要是手機(jī)中關(guān)于閃光燈,LED等相關(guān)的服務(wù);也是會(huì)調(diào)用LightsService的構(gòu)造方法和onStart方法;

mSystemServiceManager.startService(LightsService.class);

然后是啟動(dòng)DisplayManagerService服務(wù)

主要是手機(jī)顯示方面的服務(wù)

mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);

然后是啟動(dòng)PackageManagerService,該服務(wù)也是android系統(tǒng)中一個(gè)比較重要的服務(wù)

包括多apk文件的安裝,解析,刪除,卸載等等操作。

可以看到PackageManagerService服務(wù)的啟動(dòng)方式與其他服務(wù)的啟動(dòng)方式有一些區(qū)別,直接調(diào)用了PackageManagerService的靜態(tài)main方法

Slog.i(TAG, "Package Manager");
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
        mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
mFirstBoot = mPackageManagerService.isFirstBoot();
mPackageManager = mSystemContext.getPackageManager();

看一下其main方法的具體實(shí)現(xiàn):

可以看到也是直接使用new的方式創(chuàng)建了一個(gè)PackageManagerService對(duì)象,并在其構(gòu)造方法中初始化相關(guān)變量,最后調(diào)用了ServiceManager.addService方法,主要是通過Binder機(jī)制與JNI層交互

public static PackageManagerService main(Context context, Installer installer,
        boolean factoryTest, boolean onlyCore) {
    PackageManagerService m = new PackageManagerService(context, installer,
            factoryTest, onlyCore);
    ServiceManager.addService("package", m);
    return m;
}

然后查看startCoreServices方法:

可以看到這里啟動(dòng)了BatteryService(電池相關(guān)服務(wù)),UsageStatsService,WebViewUpdateService服務(wù)等。

private void startCoreServices() {
    // Tracks the battery level.  Requires LightService.
    mSystemServiceManager.startService(BatteryService.class);

    // Tracks application usage stats.
    mSystemServiceManager.startService(UsageStatsService.class);
    mActivityManagerService.setUsageStatsManager(
            LocalServices.getService(UsageStatsManagerInternal.class));
    // Update after UsageStatsService is available, needed before performBootDexOpt.
    mPackageManagerService.getUsageStatsIfNoPackageUsageInfo();

    // Tracks whether the updatable WebView is in a ready state and watches for update installs.
    mSystemServiceManager.startService(WebViewUpdateService.class);
}

總結(jié):

SystemServer進(jìn)程是android中一個(gè)很重要的進(jìn)程由Zygote進(jìn)程啟動(dòng);

SystemServer進(jìn)程主要用于啟動(dòng)系統(tǒng)中的服務(wù);

SystemServer進(jìn)程啟動(dòng)服務(wù)的啟動(dòng)函數(shù)為main函數(shù);

SystemServer在執(zhí)行過程中首先會(huì)初始化一些系統(tǒng)變量,加載類庫(kù),創(chuàng)建Context對(duì)象,創(chuàng)建SystemServiceManager對(duì)象等之后才開始啟動(dòng)系統(tǒng)服務(wù);

SystemServer進(jìn)程將系統(tǒng)服務(wù)分為三類:boot服務(wù),core服務(wù)和other服務(wù),并逐步啟動(dòng)

SertemServer進(jìn)程在嘗試啟動(dòng)服務(wù)之前會(huì)首先嘗試與Zygote建立socket通訊,只有通訊成功之后才會(huì)開始嘗試啟動(dòng)服務(wù);

創(chuàng)建的系統(tǒng)服務(wù)過程中主要通過SystemServiceManager對(duì)象來管理,通過調(diào)用服務(wù)對(duì)象的構(gòu)造方法和onStart方法初始化服務(wù)的相關(guān)變量;

服務(wù)對(duì)象都有自己的異步消息對(duì)象,并運(yùn)行在多帶帶的線程中;

參考博客

https://www.jianshu.com/p/064...

http://blog.csdn.net/qq_23547...

http://www.xuebuyuan.com/2178...

https://www.jianshu.com/p/e69...

http://blog.csdn.net/luosheng...

http://blog.csdn.net/ericming...

關(guān)于其他內(nèi)容介紹 01.關(guān)于博客匯總鏈接

1.技術(shù)博客匯總

2.開源項(xiàng)目匯總

3.生活博客匯總

4.喜馬拉雅音頻匯總

5.其他匯總

02.關(guān)于我的博客

我的個(gè)人站點(diǎn):www.yczbj.org,www.ycbjie.cn

github:https://github.com/yangchong211

知乎:https://www.zhihu.com/people/...

簡(jiǎn)書:http://www.jianshu.com/u/b7b2...

csdn:http://my.csdn.net/m0_37700275

喜馬拉雅聽書:http://www.ximalaya.com/zhubo...

開源中國(guó):https://my.oschina.net/zbj161...

泡在網(wǎng)上的日子:http://www.jcodecraeer.com/me...

郵箱:yangchong211@163.com

阿里云博客:https://yq.aliyun.com/users/a... 239.headeruserinfo.3.dT4bcV

segmentfault頭條:https://segmentfault.com/u/xi...

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/72062.html

相關(guān)文章

  • 馬蜂窩 iOS App 啟動(dòng)治理:回歸用戶體驗(yàn)

    摘要:馬蜂窩旅游歷經(jīng)幾十個(gè)版本的開發(fā)迭代,在啟動(dòng)流程上積累了一定的技術(shù)債務(wù)。我們定義啟動(dòng)廣告曝光率啟動(dòng)廣告曝光啟動(dòng)廣告加載。 增長(zhǎng)、活躍、留存是移動(dòng) App 的常見核心指標(biāo),直接反映一款 App 甚至一個(gè)互聯(lián)網(wǎng)公司運(yùn)行的健康程度和發(fā)展動(dòng)能。啟動(dòng)流程的體驗(yàn)決定了用戶的第一印象,在一定程度上影響了用戶活躍度和留存率。因此,確保啟動(dòng)流程的良好體驗(yàn)至關(guān)重要。 「馬蜂窩旅游」App 是馬蜂窩為用戶提供...

    Jinkey 評(píng)論0 收藏0
  • koa2 總體流程原理淺析(一) 之 koa 啟動(dòng)服務(wù)器解析

    摘要:?jiǎn)?dòng)流程主要的啟動(dòng)流程就是下面的步引入包實(shí)例化編寫中間件監(jiān)聽服務(wù)器引入包引入包其實(shí)就是引入的一個(gè)繼承于原生的類的類其中就包含了等原型方法實(shí)例化執(zhí)行,將等對(duì)象封裝在實(shí)例中編寫中間件首先判斷的類型,不是方法直接拋錯(cuò)是生成器函數(shù)的話用封裝是函數(shù) 啟動(dòng)流程 koa 主要的啟動(dòng)流程就是下面的 4 步:引入 koa 包 => 實(shí)例化 koa => 編寫中間件 => 監(jiān)聽服務(wù)器 const koa ...

    fsmStudy 評(píng)論0 收藏0
  • Laravel 啟動(dòng)流程

    摘要:年月日階段劃分請(qǐng)求到響應(yīng)的整個(gè)執(zhí)行階段歸納為個(gè)程序啟動(dòng)準(zhǔn)備階段文件自動(dòng)加載服務(wù)容器實(shí)例化基礎(chǔ)服務(wù)提供者的注冊(cè)核心類的實(shí)例化請(qǐng)求實(shí)例化階段實(shí)例化實(shí)例請(qǐng)求處理階段準(zhǔn)備請(qǐng)求處理的環(huán)境將請(qǐng)求實(shí)例通過中間件處理及通過路由和控制器的分發(fā)控制響應(yīng)發(fā)送和 Last-Modified: 2019年5月10日16:19:07 階段劃分 Laravel 5.5請(qǐng)求到響應(yīng)的整個(gè)執(zhí)行階段歸納為 4 個(gè): ...

    VPointer 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<