FileDocCategorySizeDatePackage
SystemService.javaAPI DocAndroid 5.1 API7104Thu Mar 12 22:22:42 GMT 2015com.android.server

SystemService

public abstract class SystemService extends Object
The base class for services running in the system process. Override and implement the lifecycle event callback methods as needed.

The lifecycle of a SystemService:

  • The constructor is called and provided with the system {@link Context} to initialize the system service.
  • {@link #onStart()} is called to get the service running. The service should publish its binder interface at this point using {@link #publishBinderService(String, IBinder)}. It may also publish additional local interfaces that other services within the system server may use to access privileged internal functions.
  • Then {@link #onBootPhase(int)} is called as many times as there are boot phases until {@link #PHASE_BOOT_COMPLETE} is sent, which is the last boot phase. Each phase is an opportunity to do special work, like acquiring optional service dependencies, waiting to see if SafeMode is enabled, or registering with a service that gets started after this one.

NOTE: All lifecycle methods are called from the system server's main looper thread.

{@hide}

Fields Summary
public static final int
PHASE_WAIT_FOR_DEFAULT_DISPLAY
public static final int
PHASE_LOCK_SETTINGS_READY
After receiving this boot phase, services can obtain lock settings data.
public static final int
PHASE_SYSTEM_SERVICES_READY
After receiving this boot phase, services can safely call into core system services such as the PowerManager or PackageManager.
public static final int
PHASE_ACTIVITY_MANAGER_READY
After receiving this boot phase, services can broadcast Intents.
public static final int
PHASE_THIRD_PARTY_APPS_CAN_START
After receiving this boot phase, services can start/bind to third party apps. Apps will be able to make Binder calls into services at this point.
public static final int
PHASE_BOOT_COMPLETED
After receiving this boot phase, services can allow user interaction with the device. This phase occurs when boot has completed and the home application has started. System services may prefer to listen to this phase rather than registering a broadcast receiver for ACTION_BOOT_COMPLETED to reduce overall latency.
private final android.content.Context
mContext
Constructors Summary
public SystemService(android.content.Context context)
Initializes the system service.

Subclasses must define a single argument constructor that accepts the context and passes it to super.

param
context The system server context.


                                     
       
        mContext = context;
    
Methods Summary
protected final android.os.IBindergetBinderService(java.lang.String name)
Get a binder service by its name.

        return ServiceManager.getService(name);
    
public final android.content.ContextgetContext()
Gets the system context.

        return mContext;
    
protected final TgetLocalService(java.lang.Class type)
Get a local service by interface.

        return LocalServices.getService(type);
    
private SystemServiceManagergetManager()

        return LocalServices.getService(SystemServiceManager.class);
    
public final booleanisSafeMode()
Returns true if the system is running in safe mode. TODO: we should define in which phase this becomes valid

        return getManager().isSafeMode();
    
public voidonBootPhase(int phase)
Called on each phase of the boot process. Phases before the service's start phase (as defined in the @Service annotation) are never received.

param
phase The current boot phase.

public voidonCleanupUser(int userHandle)
Called when an existing user is stopping, for system services to finalize any per-user state they maintain for running users. This is called after all application process teardown of the user is complete.

param
userHandle The identifier of the user.

public abstract voidonStart()
Called when the dependencies listed in the @Service class-annotation are available and after the chosen start phase. When this method returns, the service should be published.

public voidonStartUser(int userHandle)
Called when a new user is starting, for system services to initialize any per-user state they maintain for running users.

param
userHandle The identifier of the user.

public voidonStopUser(int userHandle)
Called when an existing user is stopping, for system services to finalize any per-user state they maintain for running users. This is called prior to sending the SHUTDOWN broadcast to the user; it is a good place to stop making use of any resources of that user (such as binding to a service running in the user).

param
userHandle The identifier of the user.

public voidonSwitchUser(int userHandle)
Called when switching to a different foreground user, for system services that have special behavior for whichever user is currently in the foreground. This is called before any application processes are aware of the new user.

param
userHandle The identifier of the user.

protected final voidpublishBinderService(java.lang.String name, android.os.IBinder service)
Publish the service so it is accessible to other services and apps.

        publishBinderService(name, service, false);
    
protected final voidpublishBinderService(java.lang.String name, android.os.IBinder service, boolean allowIsolated)
Publish the service so it is accessible to other services and apps.

        ServiceManager.addService(name, service, allowIsolated);
    
protected final voidpublishLocalService(java.lang.Class type, T service)
Publish the service so it is only accessible to the system process.

        LocalServices.addService(type, service);