zoukankan      html  css  js  c++  java
  • 源码心德`Context`类

          Context,中文直译为“上下文”,SDK中对其说明如下:

             Interface to global information about an application environment. This is an abstract class whose implementation

      is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls 

      for application-level operations such as launching activities, broadcasting and receiving intents, etc

        从上可知一下三点,即:

            1、它描述的是一个应用程序环境的信息,即上下文。

            2、该类是一个抽象(abstract class)类,Android提供了该抽象类的具体实现类(后面我们会讲到是ContextIml类)。

            3、通过它我们可以获取应用程序的资源和类,也包括一些应用级别操作,例如:启动一个Activity,发送广播,接受Intent

          信息 等。。

     一、Context相关类的继承关系

                             

      相关类介绍:

       Context类    路径: /frameworks/base/core/java/android/content/Context.java

                说明:  抽象类,提供了一组通用的API。

     

      ContextIml.java类  路径 :/frameworks/base/core/java/android/app/ContextImpl.java

              说明:该Context类的实现类为ContextIml,该类实现了Context类的功能。请注意,该函数的大部分功能都是直接调用

          其属性mPackageInfo去完成,这点我们后面会讲到。    

             源代码(部分)如下:

     

     ContextWrapper类 路径 :frameworksasecorejavaandroidcontentContextWrapper.java

            说明: 正如其名称一样,该类只是对Context类的一种包装,该类的构造函数包含了一个真正的Context引用,即ContextIml

           对象。    源代码(部分)如下:

       ContextThemeWrapper类 路径:/frameworks/base/core/java/android/view/ContextThemeWrapper.java

          说明:该类内部包含了主题(Theme)相关的接口,即android:theme属性指定的。只有Activity需要主题,Service不需要主题,

       所以Service直接继承于ContextWrapper类。

          源代码(部分)如下:

     

    二、 什么时候创建Context实例 

          熟悉了Context的继承关系后,我们接下来分析应用程序在什么情况需要创建Context对象的?应用程序创建Context实例的

     情况有如下几种情况:

          1、创建Application 对象时, 而且整个App共一个Application对象

          2、创建Service对象时

          3、创建Activity对象时

        因此应用程序App共有的Context数目公式为:

                         总Context实例个数 = Service个数 + Activity个数 + 1(Application对应的Context实例)

    字段  段前面第一个大写单词一般就是在我们用到的变量名字:如`MODE_APPEND`,MODE代表的就是创建和打开文件的方式。

  • 相关阅读:
    Java高级之类结构的认识
    14.8.9 Clustered and Secondary Indexes
    14.8.4 Moving or Copying InnoDB Tables to Another Machine 移动或者拷贝 InnoDB 表到另外机器
    14.8.3 Physical Row Structure of InnoDB Tables InnoDB 表的物理行结构
    14.8.2 Role of the .frm File for InnoDB Tables InnoDB 表得到 .frm文件的作用
    14.8.1 Creating InnoDB Tables 创建InnoDB 表
    14.7.4 InnoDB File-Per-Table Tablespaces
    14.7.2 Changing the Number or Size of InnoDB Redo Log Files 改变InnoDB Redo Log Files的数量和大小
    14.7.1 Resizing the InnoDB System Tablespace InnoDB 系统表空间大小
    14.6.11 Configuring Optimizer Statistics for InnoDB 配置优化统计信息用于InnoDB
  • 原文地址:https://www.cnblogs.com/ganwei/p/4740977.html
Copyright © 2011-2022 走看看