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很有用。

转载于:https://blog.51cto.com/255135/816079

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. MyBatis传入参数与parameterType
  2. src is not broadcastable to dst, but they have the same number of elements
  3. androidHandler讲解
  4. 交易劫持、钓鱼网站、盗号***严重威胁网购安全
  5. Pytorch和Numpy的默认类型
  6. 从一个帖子看部分大学生的学习心态
  7. 翁恺c语言第6周编程答案,程序设计入门——C语言 第6周编程练习 1 分解质因数(5分)(示例代码)...
  8. 一步步编写操作系统 13 栈
  9. LeetCode 926. 将字符串翻转到单调递增(动态规划)
  10. 2017福建夏令营Day7(数论)
  11. Oracle Dba手记(读书笔记)
  12. IDEA回到光标的上一次位置
  13. 在CAD中容易混淆的概念
  14. Mac 下Axure RP9下载与安装
  15. 泰然金融牵手快公益 花儿计划国学课入畲乡
  16. Vue + Element UI 实现权限管理系统(更换皮肤主题)
  17. win10dnf服务器未响应,win101903玩DNF卡顿 ,掉线,死机的解决办法
  18. 取消华为mate30 删除图片时手机弹出提示:“..检测xx删除了图片..“
  19. OP向左,SaaS向右,如何选择?
  20. element ui的双层el-dialog样式控制

热门文章

  1. 职场求生:老板说,解决不了用户流失,就要解决我,咋办?
  2. django学习(2)----APP
  3. 快速解决正则----模糊匹配、字符类、量词
  4. 关于类的非静态函数指针成员变量
  5. 网络爬虫(一):配置selenium、pycharm(windows平台)
  6. 一道关于 ARRAY 深度展开的面试题
  7. Tomcat7调试运行环境搭建与源代码分析入门
  8. mysql怎么通过frm和ibd文件还原数据
  9. 学院派CAD工具箱及CAD调用外部应用程序的方法
  10. Design Patterns(二十一):State Pattern--VB代码