源码位置:frameworks/base/core/java/com/android/server/LocalServices.java

 23 /**
 24  * This class is used in a similar way as ServiceManager, except the services registered here
 25  * are not Binder objects and are only available in the same process.
 26  *
 27  * Once all services are converted to the SystemService interface, this class can be absorbed
 28  * into SystemServiceManager.
 29  *
 30  * {@hide}
 31  */
 32 public final class LocalServices {
 33     private LocalServices() {}
 34 
 35     private static final ArrayMap<Class<?>, Object> sLocalServiceObjects =
 36             new ArrayMap<Class<?>, Object>();
 37 
 38     /**
 39      * Returns a local service instance that implements the specified interface.
 40      *
 41      * @param type The type of service.
 42      * @return The service object.
 43      */
 44     @SuppressWarnings("unchecked")
 45     public static <T> T getService(Class<T> type) {
 46         synchronized (sLocalServiceObjects) {
 47             return (T) sLocalServiceObjects.get(type);
 48         }
 49     }
 50 
 51     /**
 52      * Adds a service instance of the specified interface to the global registry of local services.
 53      */
 54     public static <T> void addService(Class<T> type, T service) {
 55         synchronized (sLocalServiceObjects) {
 56             if (sLocalServiceObjects.containsKey(type)) {
 57                 throw new IllegalStateException("Overriding service registration");
 58             }
 59             sLocalServiceObjects.put(type, service);
 60         }
       }
        ...
}

0 条评论

发表回复

您的电子邮箱地址不会被公开。