1 Service essential structure
We are still fromServiceStarting with the analysis of the fundamental significance of the service, the essence of the service is to respond to client requests. To provide services, you must establish a framework for receiving requests, processing requests, and responding to customer service. I think the Android Service designer will always keep this Service's essential block diagram in mind. From the perspective of the program, the service must have a closed loop framework and a request processing framework.
After analyzing the service box clearly, you must understand the following mechanisms and their composition.
1) Where is the closed loop structure placed?
2) How do I distribute and manage requests?
3) How is the processing framework established?
4) How is the conceptual framework established?
2 Service Basic Framework Analysis
In Android design, Native Service and Android Service adopt the same closed loop framework. This closed loop framework is placed in the C ++ space of Native.
In the service framework, ProcessState is a public part. The main framework of this public part is the closed loop framework and the Processing Framework for receiving requests from the Binder. We use ProcessSate to represent the service framework. In short:
1) addservice
2) Establish a closed loop Processing framework.
Java code:
- Int main (int argc, char ** argv ){
- Sp <ProcessState> proc (ProcessState: self ());
- AddService (String16 ("xxx0"), new xxx0Service ());
- AddService (String16 ("xxx1"), new xxx1Service ());
- ProcessState: self ()-> startThreadPool ();
- IPCThreadState: self ()-> joinThreadPool (); // Closed Loop framework
- }
2.1 Native Service
Native Service is a Service established through the Init. rc Script in the system Init stage.
Java code:
- int main(int argc, char** argv){
- sp<ProcessState> proc(ProcessState::self());
- sp<IServiceManager> sm = defaultServiceManager();
- LOGI("ServiceManager: %p", sm.get());
- AudioFlinger::instantiate();
- MediaPlayerService::instantiate();
- CameraService::instantiate();
- AudioPolicyService::instantiate();
- ProcessState::self()->startThreadPool();
- IPCThreadState::self()->joinThreadPool();
- }
1) The service process establishes a ProcessState object and registers the object in the context of the process.
2) create a new AudioFlinger object and register the object in the Service Manager Service.
3) at the beginning, the system receives, processes, and responds to the closed loop framework.
Android Service 2.2
Androids service is a service established during System Phase 2 Init2) initialization.
The circular structure is not visible in SystemServer. java, but the init2 implementation function is created, a lot of services are created, and AddService is sent to service Manager.
Init1 () is implemented in the Native space com_andoird_server_systemServer.cpp ). As soon as we look at this function, we can see that the original Closed Loop Processing Framework is here:
Init1-> system_init () @ System_init.cpp
In system_init (), we can see this long-overdue Closed Loop management framework.
Java code:
- {
- Call "com/android/server/SystemServer", "init2"
- ProcessState::self()->startThreadPool();
- IPCThreadState::self()->joinThreadPool();
- }
Basic Service Broker application (I)
Basic Service Broker application (bottom)
Introduction to the implementation of remote Service Broker
Android development tour: Android Architecture
Build an Android Application Development Environment