(M)getSystemService解析

经常看到有类似于如下的语句,之前不知道这个究竟是如何返回一个TelephonyManager对象的,今天简单地看了下,发现其实这个也没有太过复杂

(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
主要是调用了Context的getSystemService方法

public abstract Object getSystemService(@ServiceName @NonNull String name);
这个地方是一个抽象方法,那么一定是在其子类中调用的,因此ContextImpl.java文件中

@Override
    public Object getSystemService(String name) {
        return SystemServiceRegistry.getSystemService(this, name);
    }
好,继续看SystemServiceRegistry的getSystemService方法

/**
     * Gets a system service from a given context.
     */
    public static Object getSystemService(ContextImpl ctx, String name) {
        ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
        return fetcher != null ? fetcher.getService(ctx) : null;
    }
那么,SYSTEM_SERVICE_FETCHERS是什么?

/**
     * Statically registers a system service with the context.
     * This method must be called during static initialization only.
     */
    private static <T> void registerService(String serviceName, Class<T> serviceClass,
            ServiceFetcher<T> serviceFetcher) {
        SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
        SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
    }
是一个private方法,在哪儿调用的呢?

static {
    ......
    registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
                new CachedServiceFetcher<TelephonyManager>() {
            @Override
            public TelephonyManager createService(ContextImpl ctx) {
                return new TelephonyManager(ctx.getOuterContext());
            }});
    ......
}
回到registerService方法,SYSTEM_SERVICE_FETCHERS中添加的是这个static的CachedServiceFetcher对象,getSystemService返回的应该是fetcher.getService(ctx)才对,那么看一下CachedServiceFetcher类的getService方法

/**
     * Override this class when the system service constructor needs a
     * ContextImpl and should be cached and retained by that context.
     */
    static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
        private final int mCacheIndex;

        public CachedServiceFetcher() {
            mCacheIndex = sServiceCacheSize++;
        }

        @Override
        @SuppressWarnings("unchecked")
        public final T getService(ContextImpl ctx) {
            final Object[] cache = ctx.mServiceCache;
            synchronized (cache) {
                // Fetch or create the service.
                Object service = cache[mCacheIndex];
                if (service == null) {
                    service = createService(ctx);
                    cache[mCacheIndex] = service;
                }
                return (T)service;
            }
        }

        public abstract T createService(ContextImpl ctx);
    }
getService方法中调用了createService方法,而createService方法是在SystemServiceRegistry类的静态区域中定义的,回到上述registerService调用的地方

registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
                new CachedServiceFetcher<TelephonyManager>() {
            @Override
            public TelephonyManager createService(ContextImpl ctx) {
                return new TelephonyManager(ctx.getOuterContext());
            }});
createService返回的是新建的一个TelephonyManager对象,而CachedServiceFetcher的getService方法中return的就是这个

所以

(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
这个返回的就是new TelephonyManager(context)对象了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值