July 9, 2025
7 min read

Access Custom System Service from a Third-Party App

AOSP
Android
System Service
Third-Party App
SELinux
SSamir Dubey
Samir Dubey

AOSP Engineer

A step-by-step guide on accessing a custom AOSP system service (SdMathService) from a third-party app (MathApp), including UI setup, service communication, and SELinux configuration.

In the Android operating system, system services play a crucial role in providing various functionalities to both system and third-party apps. These services are designed to be long-running background tasks that can be accessed by different components across the system. In a previous article, we explored how to create a custom system service called “SdMathService,” which offers math operations like addition and multiplication.

Now, in this article, We will see how we can access the Custom System Service that we have already created in this article.— https://medium.com/@samirdubey/mastering-system-services-a-comprehensive-guide-for-android-aosp-developers-458cf35fbe73

Now we will focus on how to access the custom “SdMathService” from a 3rd party app called “MathApp.” The MathApp is not a system app, but by following a few steps, we can enable it to utilize the math operations provided by the “SdMathService.”

MainActivity.java

The MainActivity of “MathApp” is responsible for handling the UI and user interactions. We import the necessary components and initialize the views. The most crucial part is accessing the custom “SdMathService” using SdMathServiceManager. We retrieve the SdMathServiceManager instance using getSystemService(“sdmath”), where “sdmath” is the name of our custom system service. This allows “MathApp” to communicate with the “SdMathService” and perform math operations.

activity_main.xml:

This XML layout file defines the UI elements of the “MathApp” MainActivity. It includes two EditText views to enter numbers, a Button to trigger the calculations, and a TextView to display the results of addition and multiplication.

SdMathServiceManager.java

It is Important that the package of the SdMathServiceManager should be same as it is in actual SdMathServiceManager under framework/base

This class represents the custom SdMathServiceManager. We currently have a placeholder implementation for the add and multiply methods, which return 0. In the actual implementation, these methods will be responsible for communicating with the “SdMathService” and obtaining the results of math operations.

Add Selinux rule

root Path — AOSP/system/sepolicy

untrusted_app.te

This line is a SELinux policy rule that grants the “untrusted_app” domain the permission to find the “sdmath_service” in the service manager. The “untrusted_app” domain represents third-party apps installed on the Android device. With this rule, third-party apps can discover and access the “sdmath_service” system service using the service manager, enabling them to perform mathematical functions securely and efficiently.

Conclusion:

In this article, we explored how to access a custom system service (“SdMathService”) from a 3rd party app (“MathApp”). By creating a custom SdMathServiceManager within the “MathApp” package and utilizing getSystemService(“sdmath”), we successfully established communication with the “SdMathService.” This allows “MathApp” to perform addition and multiplication operations provided by the system service, extending its functionalities beyond those available in regular user-level apps.