public abstract class SystemService extends Object
The lifecycle of a SystemService:
Context
to initialize the system service.
onStart()
is called to get the service running. The service should
publish its binder interface at this point using
publishBinderService(String, IBinder)
. It may also publish additional
local interfaces that other services within the system server may use to access
privileged internal functions.
onBootPhase(int)
is called as many times as there are boot phases
until PHASE_BOOT_COMPLETED
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.
Modifier and Type | Field and Description |
---|---|
static int |
PHASE_ACTIVITY_MANAGER_READY
After receiving this boot phase, services can broadcast Intents.
|
static int |
PHASE_BOOT_COMPLETED
After receiving this boot phase, services can allow user interaction with the device.
|
static int |
PHASE_LOCK_SETTINGS_READY
After receiving this boot phase, services can obtain lock settings data.
|
static int |
PHASE_SYSTEM_SERVICES_READY
After receiving this boot phase, services can safely call into core system services
such as the PowerManager or PackageManager.
|
static int |
PHASE_THIRD_PARTY_APPS_CAN_START
After receiving this boot phase, services can start/bind to third party apps.
|
static int |
PHASE_WAIT_FOR_DEFAULT_DISPLAY |
Constructor and Description |
---|
SystemService(Context context)
Initializes the system service.
|
Modifier and Type | Method and Description |
---|---|
protected IBinder |
getBinderService(String name)
Get a binder service by its name.
|
Context |
getContext()
Gets the system context.
|
protected <T> T |
getLocalService(Class<T> type)
Get a local service by interface.
|
boolean |
isSafeMode()
Returns true if the system is running in safe mode.
|
void |
onBootPhase(int phase)
Called on each phase of the boot process.
|
void |
onCleanupUser(int userHandle)
Called when an existing user is stopping, for system services to finalize any per-user
state they maintain for running users.
|
abstract void |
onStart()
Called when the dependencies listed in the @Service class-annotation are available
and after the chosen start phase.
|
void |
onStartUser(int userHandle)
Called when a new user is starting, for system services to initialize any per-user
state they maintain for running users.
|
void |
onStopUser(int userHandle)
Called when an existing user is stopping, for system services to finalize any per-user
state they maintain for running users.
|
void |
onSwitchUser(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.
|
void |
onUnlockUser(int userHandle)
Called when an existing user is unlocked.
|
protected void |
publishBinderService(String name,
IBinder service)
Publish the service so it is accessible to other services and apps.
|
protected void |
publishBinderService(String name,
IBinder service,
boolean allowIsolated)
Publish the service so it is accessible to other services and apps.
|
protected <T> void |
publishLocalService(Class<T> type,
T service)
Publish the service so it is only accessible to the system process.
|
public static final int PHASE_WAIT_FOR_DEFAULT_DISPLAY
public static final int PHASE_LOCK_SETTINGS_READY
public static final int PHASE_SYSTEM_SERVICES_READY
public static final int PHASE_ACTIVITY_MANAGER_READY
public static final int PHASE_THIRD_PARTY_APPS_CAN_START
public static final int PHASE_BOOT_COMPLETED
public SystemService(Context context)
Subclasses must define a single argument constructor that accepts the context and passes it to super.
context
- The system server context.public final Context getContext()
public final boolean isSafeMode()
public abstract void onStart()
public void onBootPhase(int phase)
phase
- The current boot phase.public void onStartUser(int userHandle)
userHandle
- The identifier of the user.public void onUnlockUser(int userHandle)
userHandle
- The identifier of the user.public void onSwitchUser(int userHandle)
userHandle
- The identifier of the user.public void onStopUser(int userHandle)
userHandle
- The identifier of the user.public void onCleanupUser(int userHandle)
userHandle
- The identifier of the user.protected final void publishBinderService(String name, IBinder service)
protected final void publishBinderService(String name, IBinder service, boolean allowIsolated)
protected final IBinder getBinderService(String name)
protected final <T> void publishLocalService(Class<T> type, T service)
protected final <T> T getLocalService(Class<T> type)