In-depth Service Research in Android

Source: Internet
Author: User

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:

 
 
  1. Int main (int argc, char ** argv ){
  2. Sp <ProcessState> proc (ProcessState: self ());
  3. AddService (String16 ("xxx0"), new xxx0Service ());
  4. AddService (String16 ("xxx1"), new xxx1Service ());
  5. ProcessState: self ()-> startThreadPool ();
  6. IPCThreadState: self ()-> joinThreadPool (); // Closed Loop framework
  7. }

2.1 Native Service

Native Service is a Service established through the Init. rc Script in the system Init stage.

Java code:

 
 
  1. int main(int argc, char** argv){  
  2. sp<ProcessState> proc(ProcessState::self());  
  3. sp<IServiceManager> sm = defaultServiceManager();  
  4. LOGI("ServiceManager: %p", sm.get());  
  5. AudioFlinger::instantiate();  
  6. MediaPlayerService::instantiate();  
  7. CameraService::instantiate();  
  8. AudioPolicyService::instantiate();  
  9. ProcessState::self()->startThreadPool();  
  10. 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:

 
 
  1. {  
  2. Call "com/android/server/SystemServer", "init2"  
  3. ProcessState::self()->startThreadPool();  
  4. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.