首页 > 代码库 > Android Service组件在新进程中的启动过程

Android Service组件在新进程中的启动过程

1、startService

       在Android系统匿名共享内存(Anonymous Shared Memory)Java调用接口分析,http://blog.csdn.net/jltxgcy/article/details/31414509一文,Client调用了如下代码:

public class Client extends Activity implements OnClickListener {  
     public void onCreate(Bundle savedInstanceState) {  
        .....
        startService(new Intent("shy.luo.ashmem.server")); 
        ..... 
     }
..
}


      下面我们就分析整个流程,直到执行:

public class Server extends Service {  
    public void onCreate() {  
        Log.i(LOG_TAG, "Create Memory Service...");  
  
        memoryService = new MemoryService();  
  
        try {  
            ServiceManager.addService("AnonymousSharedMemory", memoryService);  
            Log.i(LOG_TAG, "Succeed to add memory service.");  
        } catch (RuntimeException ex) {  
            Log.i(LOG_TAG, "Failed to add Memory Service.");  
            ex.printStackTrace();  
        }  
  
    }
    .....
}


2、整个过程和Android Activity组件的启动过程http://blog.csdn.net/jltxgcy/article/details/35984557,几乎一致。只是ActivityRecord换成了ServiceRecord,mActivities换成了mServices,而且少了两个步骤,见下图: