getSystemService是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。

传入的Name 返回的对象 说明
WINDOW_SERVICE  WindowManager 管理打开的窗口程序
LAYOUT_INFLATER_SERVICE LayoutInflater  取得xml里定义的view
ACTIVITY_SERVICE ActivityManager  管理应用程序的系统状态

POWER_SERVICE

PowerManger 电源的服务
ALARM_SERVICE AlarmManager 闹钟的服务

NOTIFICATION_SERVICE

NotificationManager 状态栏的服务
KEYGUARD_SERVICE  KeyguardManager  键盘锁的服务
LOCATION_SERVICE  LocationManager  位置的服务,如GPS
SEARCH_SERVICE SearchManager  搜索的服务
VEBRATOR_SERVICE  Vebrator 手机震动的服务
CONNECTIVITY_SERVICE Connectivity 网络连接的服务
WIFI_SERVICE WifiManager Wi-Fi服务
 TELEPHONY_SERVICE TeleponyManager 电话服务

getSystemService()方法是如何实现的?

在研究Android Application  Framwork层的源代码遇到一个问题

就是Activity里面的getSystemService()方法是怎么实现的?
我研究了半天没看出怎么实现的
Activity继承自ContextThemeWrapper(这里面只有部分实现),ContextThemeWrapper继承自ContextWrapper,ContextWrapper继承自Context
Context是个抽象类,getSystemService也是抽象方法
没找到在哪里实现的Context的抽象方法:getSystemService啊

我觉得会不会是系统自动调用native实现这个抽象类Context?
但是请教高手们具体是怎么实现的?

你好  在 framework 层文件 ContextImpl.java 文件里面
根据service 的类型 调用相应的 manager层
public Object getSystemService(String name) {
        if (WINDOW_SERVICE.equals(name)) {
            return WindowManagerImpl.getDefault();
        } else if (LAYOUT_INFLATER_SERVICE.equals(name)) {
            synchronized (mSync) {
                LayoutInflater inflater = mLayoutInflater;
                if (inflater != null) {
                    return inflater;
                }
                mLayoutInflater = inflater =
                    PolicyManager.makeNewLayoutInflater(getOuterContext());
                return inflater;
            }
        } else if (ACTIVITY_SERVICE.equals(name)) {
            return getActivityManager();
        } else if (INPUT_METHOD_SERVICE.equals(name)) {
            return InputMethodManager.getInstance(this);
        } else if (ALARM_SERVICE.equals(name)) {
            return getAlarmManager();
        } else if (ACCOUNT_SERVICE.equals(name)) {
            return getAccountManager();
        } else if (POWER_SERVICE.equals(name)) {
            return getPowerManager();
}

Android之getSystemService的更多相关文章

  1. android中getSystemService详解

        android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监 听是否有SD卡安装及移除,ClipboardS ...

  2. Android Service GetSystemService

    http://blog.sina.com.cn/s/blog_71d1e4fc0100o8qr.html http://blog.csdn.net/bianhaohui/article/details ...

  3. Android——getSystemService

    android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监 听是否有SD卡安装及移除,ClipboardServi ...

  4. Android操作系统服务(Context.getSystemService() )

    getSystemService是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象.下面介绍系统相应的服务: 传入 ...

  5. Android -- getSystemService

    Android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监听是否有SD卡安装及移除,ClipboardServic ...

  6. Android:getContext().getSystemService()

    一.介绍 getSystemService是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象 二.语法 Windo ...

  7. android window(二)从getSystemService到WindowManagerGlobal

    在Activity调用getSystemService(WINDOW_SERVICE) 调用的是父类ContextThemeWrapper package android.view; public c ...

  8. 【Android】 context.getSystemService()浅析

    同事在进行code review的时候问到我context中的getSystemService方法在哪实现的,他看到了一个ClipBoardManager来进行剪切板存储数据的工具方法中用到了cont ...

  9. Android指纹解锁

    Android6.0及以上系统支持指纹识别解锁功能:项目中用到,特此抽离出来,备忘. 功能是这样的:在用户将app切换到后台运行(超过一定的时长,比方说30秒),再进入程序中的时候就会弹出指纹识别的界 ...

随机推荐

  1. Length of Last Word leetocde java

    题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...

  2. 任务栈 启动模式 Task Flag launchMode MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  3. Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 解决方案

    because regular C functions work differently than the Windows API functions; their "calling con ...

  4. jQuery实现新浪微博自动底部加载的方法

    jQuery ScrollPagination plugin 是一个jQuery 实现的支持无限滚动加载数据的插件. 地址:http://andersonferminiano.com/jquerysc ...

  5. 俄罗斯水手 [C#] 对List<T>取交集、连集及差集

    ※本文使用int為例,若為使用自訂之DataModel,需實作IEquatable<T>介面才能使用 1.  取交集 (A和B都有) List A : { 1 , 2 , 3 , 5 , ...

  6. android 多线程概述

    android多线程,一直是一个麻烦的事情,要掌握它的本质,我们需要搞清楚一个问题,linux多线程的本质. 我们这篇文章,来讨论以下的议程: 了解linux的历程,了解android的异步任务机制, ...

  7. Package.json小结

        生成package.json 定位到想放置package.json的目录,运行npm init,根据提示就可以生成package.json文件,其中test command可以为空. 安装mo ...

  8. 【Nodejs】nimble或async并不能保证程序串行执行,回调是回避不了的坑

    先看一段例程: //------------------------------- // 用于创建目录 //------------------------------- function creat ...

  9. (LeetCode 41)First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...

  10. RHCSA和RHCE

    RHCSA: 红帽考试流程: RHCE(上下午) 上午:RHCSA(红帽认证系统管理员)下午:RHCE (红帽认证系统工程师) 上午考试通过,下午未通过(RHCSA)上午考试未通过,下午考试通过(没有 ...