美文网首页
Android Context简介

Android Context简介

作者: ByteCarry | 来源:发表于2019-05-22 23:49 被阅读0次

    概述

    Android程序和Java程序一个不同的地方在于Android程序不能仅仅通过一个main方法就启动起来,他还需要特定的环境,就是Context,称之为上下文环境。

    Context类的继承关系

    • Context
      • ContextImpl
      • ContextWrapper
        • Application
        • Service
        • ContextThemeWrapper
          • Activity

    可以看出Context一共有三种类型:Application、Service、Activity

    Context的类型

    Activity、Service、还有Application

    Context数量 = Activity数量 + Service数量 + 1

    任何一个Context的实例,只要调用getApplicationContext()方法都可以拿到我们的Application对象。

    Context的用途

    • 启动四大组件
    • 发送广播
    • 操作数据库
    • 弹出Dialog
    • 获取系统服务
    • 获取Resource资源
    • 获取包名等应用信息
    • 获取主线程Looper
    • …...

    参考文档

    1. Android Context完全解析,你所不知道的Context的各种细节
    2. https://developer.android.com/reference/android/content/Context

    相关文章

      网友评论

          本文标题:Android Context简介

          本文链接:https://www.haomeiwen.com/subject/nehmzqtx.html