Class RootService


public abstract class RootService extends ContextWrapper
A remote root service using native Android Binder IPC.

Important: while developing an app with RootServices, modify the run/debug configuration and check the "Always install with package manager" option if testing on Android 11+, or else the code changes will not be reflected after Android Studio's deployment.

This class is almost a complete recreation of a bound service running in a root process. Instead of using the original Context.bindService(...) methods to start and bind to a service, use the provided static methods RootService.bind(...). Because the service will not run in the same process as your application, you have to use either Messenger or AIDL to define the IPC interface for communication. Please read the official documentations for more details.

Even though a RootService is a Context of your application, the ContextImpl is not constructed in a normal way, so the functionality is much more limited compared to the normal case. Be aware of this and do not expect all context methods to work.

All RootServices launched from the same process will run in the same root process. A root service will be destroyed as soon as there are no clients bound to it. This means all services will be destroyed immediately when the client process is terminated. The library will NOT attempt to automatically restart and bind to a service after it was unbound.

Daemon Mode:
If you want the service to run in the background independent from the application lifecycle, launch the service in "Daemon Mode". Check the description of CATEGORY_DAEMON_MODE for instructions on how to do so. All services running in "Daemon Mode" will run in a daemon process created per-package that is separate from regular root services. This daemon process will be used across application re-launches, and even across different users on the device. A root service running in "Daemon Mode" will be destroyed when any client called stop(Intent), or the root service itself called stopSelf().

A root service process, including the daemon process, will terminate under these conditions:

  • When the application is updated or deleted
  • When all services running in the process are destroyed (after onDestroy() is called)
See Also: