diff options
Diffstat (limited to 'core/java/android/os/ServiceManager.java')
| -rw-r--r-- | core/java/android/os/ServiceManager.java | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/core/java/android/os/ServiceManager.java b/core/java/android/os/ServiceManager.java index c008f4d37b90..b654707a683b 100644 --- a/core/java/android/os/ServiceManager.java +++ b/core/java/android/os/ServiceManager.java @@ -16,6 +16,7 @@ package android.os; +import android.annotation.NonNull; import android.compat.annotation.UnsupportedAppUsage; import android.util.ArrayMap; import android.util.Log; @@ -140,7 +141,7 @@ public final class ServiceManager { /** * Returns a reference to a service with the given name, or throws - * {@link NullPointerException} if none is found. + * {@link ServiceNotFoundException} if none is found. * * @hide */ @@ -219,6 +220,44 @@ public final class ServiceManager { } /** + * Returns whether the specified service is declared. + * + * @return true if the service is declared somewhere (eg. VINTF manifest) and + * waitForService should always be able to return the service. + */ + public static boolean isDeclared(@NonNull String name) { + try { + return getIServiceManager().isDeclared(name); + } catch (RemoteException e) { + Log.e(TAG, "error in isDeclared", e); + return false; + } + } + + /** + * Returns the specified service from the service manager. + * + * If the service is not running, servicemanager will attempt to start it, and this function + * will wait for it to be ready. + * + * @return {@code null} only if there are permission problems or fatal errors. + */ + public static native IBinder waitForService(@NonNull String name); + + /** + * Returns the specified service from the service manager, if declared. + * + * If the service is not running, servicemanager will attempt to start it, and this function + * will wait for it to be ready. + * + * @return {@code null} if the service is not declared in the manifest, or if there are + * permission problems, or if there are fatal errors. + */ + public static IBinder waitForDeclaredService(@NonNull String name) { + return isDeclared(name) ? waitForService(name) : null; + } + + /** * Return a list of all currently running services. * @return an array of all currently running services, or <code>null</code> in * case of an exception |
