android5系统 getSystemService源码分析

先看用法,如图所示。

进去一层层的看,发现顶层是一个抽象类,如图所示。


我们看getBaseContext方法返回的是基类。

我们继续看基类发现是一个抽象函数。

而android的设计实现其实是在程序启动的时候会去设置这个mBase;

也就是调用的是这个设置好的mBase的方法。我们看代码。
程序的入口其实是类ActivityThread的main方法,如图所示。

它会创建一个对象,同时获得一个handle

 ActivityThread thread = new ActivityThread();
        thread.attach(false);

        if (sMainThreadHandler == null) {
            sMainThreadHandler = thread.getHandler();
        }


我们看它handle消息部分,代码如下:

        public void handleMessage(Message msg) {
            if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
            switch (msg.what) {
                case LAUNCH_ACTIVITY: {
                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
                    final ActivityClientRecord r = (ActivityClientRecord) msg.obj;

                    r.packageInfo = getPackageInfoNoCheck(
                            r.activityInfo.applicationInfo, r.compatInfo);
                    handleLaunchActivity(r, null);
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                } break;
                case RELAUNCH_ACTIVITY: {
                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
                    ActivityClientRecord r = (ActivityClientRecord)msg.obj;
                    handleRelaunchActivity(r);
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                } break;
                case PAUSE_ACTIVITY:
                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
                    handlePauseActivity((IBinder)msg.obj, false, (msg.arg1&1) != 0, msg.arg2,
                            (msg.arg1&2) != 0);
                    maybeSnapshot();
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                    break;
                case PAUSE_ACTIVITY_FINISHING:
                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
                    handlePauseActivity((IBinder)msg.obj, true, (msg.arg1&1) != 0, msg.arg2,
                            (msg.arg1&1) != 0);
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                    break;


有一个handleLaunchActivity方法,这个就是启动的方法,代码如下:

    private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
        // If we are getting ready to gc after going to the background, well
        // we are back active so skip it.
        unscheduleGcIdler();
        mSomeActivitiesChanged = true;

        if (r.profilerInfo != null) {
            mProfiler.setProfiler(r.profilerInfo);
            mProfiler.startProfiling();
        }

        // Make sure we are running with the most recent config.
        handleConfigurationChanged(null, null);

        if (localLOGV) Slog.v(
            TAG, "Handling launch of " + r);

        // Initialize before creating the activity
        WindowManagerGlobal.initialize();

        Activity a = performLaunchActivity(r, customIntent);


我们进入 performLaunchActivity方法:

          if (activity != null) {
                Context appContext = createBaseContextForActivity(r, activity);
                CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
                Configuration config = new Configuration(mCompatConfiguration);
                if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
                        + r.activityInfo.name + " with config " + config);
                activity.attach(appContext, this, getInstrumentation(), r.token,
                        r.ident, app, r.intent, r.activityInfo, title, r.parent,
                        r.embeddedID, r.lastNonConfigurationInstances, config,
                        r.referrer, r.voiceInteractor);

有个调用了activity.attach函数。

进入Activity.attach函数。

 final void attach(Context context, ActivityThread aThread,
            Instrumentation instr, IBinder token, int ident,
            Application application, Intent intent, ActivityInfo info,
            CharSequence title, Activity parent, String id,
            NonConfigurationInstances lastNonConfigurationInstances,
            Configuration config, String referrer, IVoiceInteractor voiceInteractor) {
        attachBaseContext(context);

调用attachBaseContext这个看源码没有搜索到,我们看编译生成的jar包。

和as的sdk对应看。


继续看它的父类会它这个上下文设置上去。

     */
    protected void attachBaseContext(Context base) {
        if (mBase != null) {
            throw new IllegalStateException("Base context already set");
        }
        mBase = base;
    }



我们回过头看ActivityThread的performLaunchActivity方法。

 if (activity != null) {
                Context appContext = createBaseContextForActivity(r, activity);

如图所示。

我们看它怎么创建的,代码:

    private Context createBaseContextForActivity(ActivityClientRecord r,
            final Activity activity) {
        ContextImpl appContext = ContextImpl.createActivityContext(this, r.packageInfo, r.token);
        appContext.setOuterContext(activity);
        Context baseContext = appContext;

        final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
        try {
            final int displayId = ActivityManagerNative.getDefault().getActivityDisplayId(r.token);
            if (displayId > Display.DEFAULT_DISPLAY) {
                Display display = dm.getRealDisplay(displayId, r.token);
                baseContext = appContext.createDisplayContext(display);
            }
        } catch (RemoteException e) {
        }

        // For debugging purposes, if the activity‘s package name contains the value of
        // the "debug.use-second-display" system property as a substring, then show
        // its content on a secondary display if there is one.
        String pkgName = SystemProperties.get("debug.second-display.pkg");
        if (pkgName != null && !pkgName.isEmpty()
                && r.packageInfo.mPackageName.contains(pkgName)) {
            for (int displayId : dm.getDisplayIds()) {
                if (displayId != Display.DEFAULT_DISPLAY) {
                    Display display = dm.getRealDisplay(displayId, r.token);
                    baseContext = appContext.createDisplayContext(display);
                    break;
                }
            }
        }
        return baseContext;
    }


发现new了一个ContextImpl对象因此所有实现都 在这里面,我们进去直接看发现他是去保存所有启动的服务map里获取,没有就创建对应的服务程序。

原文地址:https://blog.51cto.com/haidragon/2411049

时间: 2024-08-29 16:32:32

android5系统 getSystemService源码分析的相关文章

【精】EOS智能合约:system系统合约源码分析

系统合约在链启动阶段就会被部署,是因为系统合约赋予了EOS链资源.命名拍卖.基础数据准备.生产者信息.投票等能力.本篇文章将会从源码角度详细研究system合约. 关键字:EOS,eosio.system,智能合约,name类型,native.hpp,newaccount,bidname,core token init,onblock,更新已入选生产节点 eosio.system 概览 笔者使用的IDE是VScode,首先来看eosio.system的源码结构.如下图所示. 本文分析的源码来自于

第一次作业:基于Linux系统深入源码分析进程模型

1.前言 本文主要基于Linux 2.6源代码分析进程模型.源代码下载地址:https://elixir.bootlin.com/linux/v2.6.39/source 2.进程 定义:进程是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础. 3.Linux系统进程的组织 进程是由进程控制块(PCB).程序段.数据段三部分组成. 3.1 进程控制块 进程控制块(Processing Control Block),是操作系统核心中一种数据结构

本书已出版<拨云见日:基于android的内核与系统架构源码分析 >

已陆续倒到各大电商网站及新华书店 http://item.jd.com/11594135.html http://www.amazon.cn/%E6%8B%A8%E4%BA%91%E8%A7%81%E6%97%A5-%E5%9F%BA%E4%BA%8Eandroid%E7%9A%84%E5%86%85%E6%A0%B8%E4%B8%8E%E7%B3%BB%E7%BB%9F%E6%9E%B6%E6%9E%84%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90-%E7%8E%8

Android5.1源码分析系列(一)Settings源码分析

最近在看android源码,现在想做一个系列,专门对源码进行简单直接的分析.本人道行尚浅,希望大家能够进行批评,本人感激不尽. Android5.1 Settints源码分析 1 概要 本文分析的文件目录: /packages/apps/Settings/AndroidManifest.xml /packages/apps/settings/src/com/android/settings/Settings.java /packages/apps/settings/src/com/android

东麓庄园游戏开发系统源码分析

东麓庄园游戏开发系统源码分析 袁生:151.1222.4001(微信同步)东麓庄园游戏,东麓庄园模式,东麓庄园理财,东麓庄园软件,东麓庄园app,东麓庄园源码,东麓庄园农场,东麓庄园平台,东麓庄园开发,东麓庄园返利,东麓庄园定制开发. 注册和交易注册后最低投资66元,普通领导人帮下级玩家注册激活收66元,转660个金币,其中6元是平台管理费.到达下级会员账号600个金币.会员之间交易不收手续费,果实兑换成金币平台收取10%的手续费.东麓庄园源码开发 151.1222.4001(微信同步) 东麓庄

MT 6572 平台手机系统源码分析

1.Android源码中Package包下有: 2.最主要使用的是Package/app 下的包,这里面包含一些系统应用,比如: 3.编译app模块下的应用的方法如下: 编译方法: User版本: ./mk -o=TARGET_BUILD_VARIANT=user ${projectname} mm packages/apps/Settings/ Eng版本: ./mk ${projectname} mm packages/apps/Settings/ 4.在package/app 下内置第三方

Android应用setContentView与LayoutInflater加载解析机制源码分析

[工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处,尊重分享成果] 1 背景 其实之所以要说这个话题有几个原因: 理解xml等控件是咋被显示的原理,通常大家写代码都是直接在onCreate里setContentView就完事,没怎么关注其实现原理. 前面分析<Android触摸屏事件派发机制详解与源码分析三(Activity篇)>时提到了一些关于布局嵌套的问题,当时没有深入解释. 所以接下来主要分析的就是View或者ViewGroup对象是如何添加至应用程

Android应用Activity、Dialog、PopWindow窗口显示机制及源码分析

[工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处,尊重劳动成果] 1 背景 之所以写这一篇博客的原因是因为之前有写过一篇<Android应用setContentView与LayoutInflater加载解析机制源码分析>,然后有人在文章下面评论和微博私信中问我关于Android应用Dialog.PopWindow.Toast加载显示机制是咋回事,所以我就写一篇文章来分析分析吧(本文以Android5.1.1 (API 22)源码为基础分析),以便大家在应

Android应用Activity、Dialog、PopWindow、Toast窗口添加机制及源码分析

1  背景 之所以写这一篇博客的原因是因为之前有写过一篇<Android应用setContentView与LayoutInflater加载解析机制源码分析>, 然后有人在文章下面评论和微博私信中问我关于Android应用Activity.Dialog.PopWindow加载显示机制是咋回事,所以我就写一 篇文章来分析分析吧(本文以Android5.1.1 (API 22)源码为基础分析),以便大家在应用层开发时不再迷糊. PS一句:不仅有人微博私信我这个问题,还有人问博客插图这些是用啥画的,这