Context字面意思是上下文,位于framework package的android.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄。很多方法需要通过 Context才能识别调用者的实例:比如说Toast的第一个参数就是Context,一般在Activity中我们直接用this代替,代表调用者的实例为Activity,而到了一个button的onClick(View view)等方法时,我们用this时就会报错,所以我们可能使用ActivityName.this来解决,主要原因是因为实现Context的类主要有Android特有的几个模型,Activity以及Service。

Context提供了关于应用环境全局信息的接口。它是一个抽象类,它的执行被Android系统所提供。它允许获取以应用为特征的资源和类型。同时启动应用级的操作,如启动Activity,broadcasting和接收intents。

下面介绍Context的一些get方法,通过这些get方法可以获取应用环境全局信息:

1.public abstract Context getApplicationContext ()

Return the context of the single, global Application object of the current process.

2.public abstract ApplicationInfo getApplicationInfo ()

Return the full application info for this context's package.

3.public abstract ContentResolver getContentResolver ()

Return a ContentResolver instance for your application's package.

4.public abstract PackageManager getPackageManager ()

Return PackageManager instance to find global package information.

5.public abstract String getPackageName ()

Return the name of this application's package.

6.public abstract Resources getResources ()

Return a Resources instance for your application's package.

7.public abstract SharedPreferences getSharedPreferences (String name, int mode)

Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.

8.public final String getString (int resId)

Return a localized string from the application's package's default string table.

9.public abstract Object getSystemService (String name)

Return the handle to a system-level service by name. The class of the returned object varies by the requested name. Currently available names are:

还有很多有用的方法,具体不一一列举。详情请参考文档,反正Context很有用。

以上内容转载自:http://blog.csdn.net/zhangqijie001/article/details/5891682

Android中Context简介(通俗易懂)相关推荐

  1. Android中Context简介

    Context字面意思是上下文,位于framework package的android.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄.很多方法需要通过 C ...

  2. Android中Context

    Context字面意思上下文,位于framework package的android.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄,很多方法需要通过 Co ...

  3. android context继承关系,android中Context深入详解

    以下分别通过Context认知角度,继承关系,对象创建等方面android中Context做了深入的解释,一起学习下. 1.Context认知. Context译为场景,一个应用程序可以认为是一个工作 ...

  4. Android中Context 的理解

    这里记录Context的原因是新来的同事问我Android Context 怎样理解,我是这样说的,Context 英文是上下文,它是一个抽象的类,加入在MainActivity 中,Context ...

  5. 【Android】 Android中适配器简介

    1. BaseAdapter的使用实例 BaseAdapter baseAdapter = new BaseAdapter() {@Overridepublic View getView(int po ...

  6. Android中Context详解

    给大家介绍下我们在应用开发中最熟悉而陌生的朋友-----Context类 ,说它熟悉,是应为我们在开发中时刻的在与它打交道,例如:Service.BroadcastReceiver.Activity等 ...

  7. Android中GPS简介及其应用

    随时随地技术实战干货,获取项目源码.学习资料,请关注源代码社区公众号(ydmsq666) GPS是Global Positioning System(全球定位系统)的简称,它的作用就是为全球的物体提供 ...

  8. android中api简介

    1:DisplayMetrics  Display 获取屏幕的基本信息 DisplayMetrics displayMetrics=new DisplayMetrics(); Display disp ...

  9. Android中的Context理解

    1.sdk当中关于Context的介绍 Interface to global information about an application environment. This is an abs ...

最新文章

  1. SP1026 FAVDICE - Favorite Dice
  2. RAR压缩包审计工具unrar-nofree
  3. Android系统中设置TextView等的行间距
  4. [总结]Asp.net中的页面乱码的问题
  5. /GS 编译选项,_security_cookie,软件强制DEP
  6. php多图上传插件ios,yii2组件之多图上传插件FileInput的详细使用
  7. python扫描ip的端口打开情况
  8. 前端学习(1426):ajax封装
  9. 2020年中国人工智能商业落地研究报告
  10. mysql 服务账号_MySql 账号管理
  11. 微软开源深度学习优化库 DeepSpeed,可训练 1000 亿参数的模型
  12. django-admin.py startproject HelloWorld创建文件提示invalid syntax
  13. python入门基础2 if语句 while循环 for循环
  14. 21. Django进阶:内建用户系统
  15. 基于 Linux 和 MiniGUI 的嵌入式系统软件开发指南(八) MiniGUI 和其他嵌入式 Linux 上的图形及图形用户界面系统...
  16. 苏宁金科CTO肖军:如何让1000人+的团队持续打胜仗,从找人,到育人,到用人具体怎么做?...
  17. ubuntu查看 固态硬盘位置_在Ubuntu(Linux)中启用固态硬盘(固态硬盘)TRIM | MOS86...
  18. XAMPP之二-Apache无法启动解决办法
  19. PH15-7Mo是什么?
  20. 二维标准Kalman滤波

热门文章

  1. MySQL5.7 解决 字符编码问题 与 安装完 MySQL 无 my.ini
  2. Science | 基于算法设计疫苗的人工蛋白
  3. PyTorch | (1)初识PyTorch
  4. 3 calender python_python3笔记二十一:时间操作datetime和calendar
  5. ISME: 北大吴晓磊组发现囊泡为细菌利用环境血红素提供全新途径
  6. R包corrplot绘图相关系数矩阵
  7. MetaPhlAn2-增强版宏基因组分类谱工具-一条命令获得宏基因组物种组成
  8. R语言使用aov函数进行单因素协方差分析(One-way ANCOVA)、使用multcomp包的glht函数检验组均值之间所有成对对比差异、通过contrast参数自定义对比组进行组间两两方差分析
  9. seaborn可视化散点图并自定义图像的标题(title)以及自定义标题的字体大小(Set a Title to a Seaborn Plot)
  10. R语言ggplot2可视化绘制带有双y轴(double y axis)的分面图(facet、facet_grid)