Android 中 上下文Context理解

Context

1.Context的继承关系和源码分析

在这里插入图片描述在这里插入图片描述

/**
 * 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.
 */

以上是api-28中google官方对context的注释,大概翻译如下:
1.是对应用上下文的描述;
2.是抽象类;
3、通过它我们可以获取应用程序的资源和类,也包括一些应用级别操作。

Context类它有两个具体的实现子类:ContextImpl和ContextWrapper。其中ContextWrapper类,如其名所言,这只是一个包装而已,ContextWrapper构造函数中必须包含一个真正的Context引用,同时ContextWrapper中提供了attachBaseContext()用于给ContextWrapper对象中指定真正的Context对象,调用ContextWrapper的方法都会被转向其所包含的真正的Context对象。ContextThemeWrapper类,其内部包含了与主题(Theme)相关的接口,只有Activity才需要主题,Service是不需要主题的,因为Service是没有界面的后台场景,所以Service直接继承于ContextWrapper,Application同理。而ContextImpl类则真正实现了Context中的所有函数,应用程序中所调用的各种Context类的方法,其实现均来自于该类。

一、Context相关类的继承关系
源代码(部分)如下:

public abstract class Context {  
         ...  
         //获得系统级服务  
         public abstract Object getSystemService(String name);  
          //通过一个Intent启动Activity  
         public abstract void startActivity(Intent intent);    
         //启动Service 
         public abstract ComponentName startService(Intent service);   
         //根据文件名得到SharedPreferences对象  
         public abstract SharedPreferences getSharedPreferences(String name,int mode); //Activity token  弹框中有用到
          public IBinder getActivityToken();
         //
         public ApplicationInfo getApplicationIn
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值