On Android Core Services

Source: Internet
Author: User
Tags app service

Finally into the advanced stage of Android learning, the first topic is Android Core Services. First, let's get to know the core services.

Android Core Services are different from app services. The app service inherits from the service base class, which is started and bound to the activity while the app is running. The core service is to parse the init.rc files in the system when the device is powered on, earlier than any app process and app service. The app service is Activitymanangerserviec initiated and bound, and the core service must be registered in ServiceManager.

Core services are divided into native service and Android service two kinds. In general, the native service is written in C + +, mainly to achieve the function near the drive layer, Android service is written by Java, mainly to achieve the function near the app. No matter what core services are running in different processes of the app, the app needs to use IPC to communicate with the core services.

IPC is an ingenious framework for solving inter-process communication in Android, with few codes and powerful features. Process A wants to communicate with the process B where the service resides, and must first obtain the IBinder interface to the service. IBinder interface, the main implementation of inter-process Association, will be born in process a binderproxy. This proxy class, which runs on process A, will proxy all service requests to process B. For process A, it's like owning the service yourself. The client-server structure is used to achieve the cohesion-poly-low coupling perfectly.

As mentioned above, the core service is started and managed by ServiceManager, and other processes must pass ServiceManager to access a service to get the IBinder interface. When starting a core service, one of the most important is to log the core service into ManagerService, with the following code:

int r = Defaultservicemanager ()->addservice (String16 ("xxx"), new Sqrservice);

At this point, it means that the core service can be requested and bound. The request IBinder interface code is as follows:

M_ib = Sm->getservice (String16 ("xxx"));

From the point of view of soft and hard integration, the core service exists to enable upper-level Java applications to use DRIVER/HW device-specific critical plumbing. Based on the open source features of Android, different hardware manufacturers will have their own design, performance of various hardware devices, reasonable writing C-tier core services to give full play to the hardware features. The superior design on the ground floor will provide strong support for the performance and user experience of the upper-level application, which is a combination of force and beauty in the Android framework.

Design experience

The client-server structure mentioned earlier is an important idea in the IPC framework, and we can apply this idea when we are writing a small framework of JNI layers. The key is to move the control point down to the C layer, encapsulate the service program in its own frame, and cohesion the low coupling. The C layer is responsible for acquiring the IBinder interface of the core service and establishing the Java layer's Binderproxy object, returning the Binderproxy interface to the Java layer. The code is as follows:

sp<iservicemanager> sm = Defaultservicemanager ();

M_ib = Sm->getservice (String16 ("xxx");

Jobject IBJ = Javaobjectforibinder (ENV,MIB);

On Android Core Services

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.