Android 6.0
1:应用怎么使用vibrator
Vibrator mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); //获取Vibrator实例
mVibrator.vibrate(500);
frameworks/base/core/java/android/app/SystemServiceRegistry.java
public static Object getSystemService(ContextImpl ctx, String name) {
ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
return fetcher != null ? fetcher.getService(ctx) : null;
} //获取Vibrator
SYSTEM_SERVICE_FETCHERS是一个HashMap,通过我们服务的名字name字符串,从这个HashMap里取出一个ServiceFetcher,
再return这个ServiceFetcher的getService()。ServiceFetcher是什么?它的getService()又是什么?
既然他是从SYSTEM_SERVICE_FETCHERS这个HashMap里get出来的,那就找一找这个HashMap都put了什么。
通过搜索SystemServiceRegistry可以找到如下代码:
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);
}
调用
final class SystemServiceRegistry {
registerService(Context.VIBRATOR_SERVICE, Vibrator.class, //使用private static <T> void registerService
new CachedServiceFetcher<Vibrator&