Skip to main content

Android System Services Patterns

Android has around 100 system services, providing various system functionality and services. Applications run in their own process and communicate with those system services through client class, aka all kinds of Managers, which talks to the service through Binder.

In this article, we will exam Android system service implementation patterns.


1. Client: get a client to access the system services

The most common way to get a client is to call  context.getSystemService(XXXX_SERVICE)
and you will be returned an XxxxManager. Or, be explicit, call context.getPackageManager().
Finally, new a client yourself, e.g new MediaPlayer().

2. Connect : Those XxxxManagers are actually a wrapper of the proxy side of the corresponding service and they always connect to the service with Binder. Following code snippet illustrates the pattern ,assuming XxxxManager is the client of XxxxServices,

    class XxxxManager {
           IXxxxService mXxxxServcie;
           XxxxManager() {
           mXxxxServcie = IXxxxService.Stub.asInterface(
                                                ServiceManager.getService(XXXX_SERVICE_BINDER_NAME));
          }
    }

Where the IXxxxService.Stub.asInterface(ob) simply returns IXxxxxService.Stub.Proxy(obj), which is a proxy side of service. 

3. Services and Server

The services are either hosted in system_server, or one of the native servers, such as mediaserver, surfaceflinger. 

For the services hosted in the system server, there are four varieties. 
  • The service is complete java based, no native code is needed. e.g PackageManagerService.
  • The service has native code, e.g. ComsuerIrService.
  • The service in turn connects to the system daemon with socket, e.g MountService talks to vold.
  • The service connects to another native services, e.g. DisplayManagerService connects to SurfaceFlinger through SurfaceControl class. In this case, DisplayManagerService acts like a client of SurfaceFlinger.
The XxxManager can also connect to the native service directly.
  • CameraManager connects to native CameraService directly through a java wrapper to ServiceManager.
  • SensorManager connects to native SensorServices through the jni. Or, MediaPlayer connects directly to native MediaPlayerService.
Following diagram visualizes what we just talked. 
Android System Services Pattern
Please click here for a complete list of the android system services and their implementation details, including the interface file, client class and server class. Note that this is a unfinished work and I am sure it will never be finished....since the number of services is just too much.. However, once you understand the patterns explained in this article, there is no necessity of checking each and every services. They just look similar and fall into the patterns we talked here. If it is not yet, let me know and I will add it :)

Popular posts from this blog

Android Camera2 API Explained

Compared with the old camera API, the Camera2 API introduced in the L is a lot more complex: more than ten classes are involved, calls (almost always) are asynchronized, plus lots of capture controls and meta data that you feel confused about.

No worries. Let me help you out. Whenever facing a complex system need a little bit effort to understand, I usually turns to the UML class diagram to capture the big picture.

So, here is the class diagram for Camera2 API.




You are encouraged to read this Android document first and then come back to this article, with your questions. I'll expand what is said there, and list the typical steps of using camera2 API. 

1. Start from CameraManager. We use it to iterate all the cameras that are available in the system, each with a designated cameraId. Using the cameraId, we can get the properties of the specified camera device. Those properties are represented by class CameraCharacteristics. Things like "is it front or back camera", "outpu…

Java Collections Framework Cheat Sheet

Java Collections Framework (JCF) implements the Abstract Data Type  for Java platform. Every serious Java programmer should familiar himself on this topic and be able to choose the right class for specific need.  A thorough introduction to JCF is not the target of this small article and to achieve that goal you can start with this excellent tutorial . 

Instead, I'd like to
1) Provide an overview of JCF's classes ,   2) Provide a cheat sheet you can post in your cubicel for daily reference, 3) Underline the relationship between JCF's implementation and the data structure and algorithm you learned in your undergraduate course

With these goals in mind, I came up following diagram - Java Collection Cheat Sheet. You can click it to zoom in. There is no necessity for more explanation once your familiar with UML class diagram and have a basic understanding of common data structures.


Android Security: An Overview Of Application Sandbox

The Problem: Define a policy to control how various clients can access different resources. A solution: Each resource has an owner and belongs to a group.Each client has an owner but can belongs to multiple groups.Each resource has a mode stating the access permissions allowed for its owner, group members and others, respectively. In the context of operating system, or Linux specifically, the resources can be files, sockets, etc; the clients are actually processes; and we have three access permissions:read, write and execute.