Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 基於Andoird 4.2.2的Account Manager源代碼分析學習:AccountManagerService系統服務的添加

基於Andoird 4.2.2的Account Manager源代碼分析學習:AccountManagerService系統服務的添加

編輯:關於Android編程

從啟動說起

Android系統加載時,首先啟動init進程,該進程會啟動Zygote進程。Zygote進程執行/system/bin/app_process程序。app_process程序在執行中,通過AppRuntime::start()函數來創建虛擬機實例,並注冊JNI方法。

 

[cpp]
int main(int argc, const char* const argv[]) 

    ... 
    if (zygote) { 
        runtime.start("com.android.internal.os.ZygoteInit", 
                startSystemServer ? "start-system-server" : ""); 
    } 
    ... 

int main(int argc, const char* const argv[])
{
    ...
    if (zygote) {
        runtime.start("com.android.internal.os.ZygoteInit",
                startSystemServer ? "start-system-server" : "");
    }
    ...
}
看一下AppRuntime::start()函數的說明:

 

[cpp]
/*
 * Start the Android runtime.  This involves starting the virtual machine
 * and calling the "static void main(String[] args)" method in the class
 * named by "className".
 *
 * Passes the main function two arguments, the class name and the specified
 * options string.
 */ 
void AndroidRuntime::start(const char* className, const char* options) 

    ... 

/*
 * Start the Android runtime.  This involves starting the virtual machine
 * and calling the "static void main(String[] args)" method in the class
 * named by "className".
 *
 * Passes the main function two arguments, the class name and the specified
 * options string.
 */
void AndroidRuntime::start(const char* className, const char* options)
{
    ...
}
這就是說,這個函數做兩件事情:首先虛擬機,其次執行className參數提供的Java類的main()方法。
這裡傳入className是"com.android.internal.os.ZygoteInit",那麼看一下它的main()方法:

 

[java]
public static void main(String argv[]) { 
... 
    try { 
        if (argv[1].equals("start-system-server")) { 
            startSystemServer(); 
... 

    public static void main(String argv[]) {
    ...
        try {
            if (argv[1].equals("start-system-server")) {
                startSystemServer();
    ...
    }
看一下startSystemServer()方法的實現:


[java]
private static boolean startSystemServer() 
        throws MethodAndArgsCaller, RuntimeException { 
    … 
 
    int pid; 
 
    try { 
        … 
 
        /* 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) { 
        handleSystemServerProcess(parsedArgs); 
    } 
 
    return true; 

    private static boolean startSystemServer()
            throws MethodAndArgsCaller, RuntimeException {
        …

        int pid;

        try {
            …

            /* 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) {
            handleSystemServerProcess(parsedArgs);
        }

        return true;
    }
在這裡,Zygote進程fork出SystemServer進程,隨後再handleSystemServerProcess()方法中對它做初始處理,包括關閉克隆Zygote進程所帶來的socket。

接下來,調用RuntimeInit.zygoteInit()方法完成其它的初始化工作。其中,將會調用SystemServer.main()方法進入SystemServer本身的初始化。main()方法經過一系列的步驟之後,調用本地方法init1()來啟動SurfaceFlinger和SensorService這樣的關鍵服務。之後init1()從本地回調Java層的SystemServer.init2()方法來啟動Java層的各項服務:

[java]
public static final void init2() { 
    Slog.i(TAG, "Entered the Android system server!"); 
    Thread thr = new ServerThread(); 
    thr.setName("android.server.ServerThread"); 
    thr.start(); 

    public static final void init2() {
        Slog.i(TAG, "Entered the Android system server!");
        Thread thr = new ServerThread();
        thr.setName("android.server.ServerThread");
        thr.start();
    }


SystemThread是一個線程子類,在這個線程的執行中,將建立一個消息循環,接著啟動系統的各個服務,並注冊到ServiceManager中。

終於可以進入主題了。在這裡,就包括AccountManagerService的創建與注冊:


[java]
class ServerThread extends Thread { 
    private static final String TAG = "SystemServer"; 
    … 
 
    @Override 
    public void run() { 
        ... 
 
        Looper.prepare();  
        ... 
        
        AccountManagerService accountManager = null; 
        ... 
        // Critical services...  
        try { 
            ... 
            // The AccountManager must come before the ContentService  
            try { 
                Slog.i(TAG, "Account Manager"); 
                accountManager = new AccountManagerService(context); 
                ServiceManager.addService(Context.ACCOUNT_SERVICE, accountManager); 
            } catch (Throwable e) { 
                Slog.e(TAG, "Failure starting Account Manager", e); 
            } 
        ... 
   } 
   ... 

class ServerThread extends Thread {
    private static final String TAG = "SystemServer";
    …

    @Override
    public void run() {
        ...

        Looper.prepare();
        ...
      
        AccountManagerService accountManager = null;
        ...
        // Critical services...
        try {
            ...
            // The AccountManager must come before the ContentService
            try {
                Slog.i(TAG, "Account Manager");
                accountManager = new AccountManagerService(context);
                ServiceManager.addService(Context.ACCOUNT_SERVICE, accountManager);
            } catch (Throwable e) {
                Slog.e(TAG, "Failure starting Account Manager", e);
            }
        ...
   }
   ...
}

 

接下來學習AccountManagerService相關的接口與實現。

 

 

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved