type
status
date
slug
summary
tags
category
icon
password
Android中经常会通过
context.getSystemService(Context.XX_Service)
,这里简单看一下实现方式.在ContextImpl.java中有:
在SystemServiceRegistry中有:
再来看下ServiceFetcher接口的定义:
可以看到这里主要分为两个步骤:
- 从Map中根据Service name获取对应的ServiceFetcher接口实现类;
- 通过对应的ServiceFetcher创建对应的Service实现类;
那么这些map里数据都是什么时候添加的呢?
答案是在类的静态初始化块中:
其实就是三个map:
- <Class,ServiceName>
- <ServiceName,ServiceFetcher>
- <ServiceName, serviceClass.getSimpleName()>
- 作者:姜康
- 链接:https://jiangkang.tech/article/273e6767-ee1a-4242-b263-f5c815b3ea41
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。