一个简单的SystemService(vibrator)

本文详细分析了Android系统中Vibrator服务的工作原理,从如何通过`getSystemService`获取`Vibrator`实例,到`SystemServiceRegistry`如何管理服务,再到`VibratorService`的实现和注册。主要涉及`Vibrator`类、`IVibratorService`接口、`SystemVibrator`以及`VibratorService`在系统服务中的角色。
摘要由CSDN通过智能技术生成


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&

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值