Android中Context详解
来源:互联网 发布:office mac 2013 编辑:程序博客网 时间:2024/05/22 03:24
大家好, 今天给大家介绍下我们在应用开发中最熟悉而陌生的朋友-----Context类 ,说它熟悉,是应为我们在开发中
时刻的在与它打交道,例如:Service、BroadcastReceiver、Activity等都会利用到Context的相关方法 ; 说它陌生,完全是
因为我们真正的不懂Context的原理、类结构关系。一个简单的问题是,一个应用程序App中存在多少个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对象去构建应用级别操作(application-level operations) 。
一、Context相关类的继承关系
相关类介绍:
Context类 路径: /frameworks/base/core/java/android/content/Context.java
说明: 抽象类,提供了一组通用的API。
源代码(部分)如下:
1.public abstract class Context {
2. ...
3. public abstract Object getSystemService(String name); //获得系统级服务
4. public abstract void startActivity(Intent intent); //通过一个Intent启动Activity
5. public abstract ComponentName startService(Intent service); //启动Service
6. //根据文件名得到SharedPreferences对象
7. public abstract SharedPreferences getSharedPreferences(String name,int mode);
8. ...
9.}
ContextIml.java类 路径 :/frameworks/base/core/java/android/app/ContextImpl.java
说明:该Context类的实现类为ContextIml,该类实现了Context类的功能。请注意,该函数的大部分功能都是直接调用
其属性mPackageInfo去完成,这点我们后面会讲到。
源代码(部分)如下:
1./**
2. * Common implementation of Context API, which provides the base
3. * context object for Activity and other application components.
4. */
5.class ContextImpl extends Context{
6. //所有Application程序公用一个mPackageInfo对象
7. /*package*/ ActivityThread.PackageInfo mPackageInfo;
8.
9. @Override
10. public Object getSystemService(String name){
11. ...
12. else if (ACTIVITY_SERVICE.equals(name)) {
13. return getActivityManager();
14. }
15. else if (INPUT_METHOD_SERVICE.equals(name)) {
16. return InputMethodManager.getInstance(this);
17. }
18. }
19. @Override
20. public void startActivity(Intent intent) {
21. ...
22. //开始启动一个Activity
23. mMainThread.getInstrumentation().execStartActivity(
24. getOuterContext(), mMainThread.getApplicationThread(), null, null, intent, -1);
25. }
26.}
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2012-07/65037.htm
- Android中Context详解
- Android中Context详解
- Android中Context详解
- Android中Context详解
- Android中Context详解
- Android中Context详解
- Android中Context详解
- Android中Context详解
- Android中Context详解
- Android中Context详解
- Android中Context详解
- Android中Context详解
- Android中Context详解
- Android中Context详解
- Android中Context详解
- Android中Context详解
- Android 中 Context 详解
- Android中Context详解
- C++中的四种强制转换
- 条形码(一维码)工具类
- 正则表达式学习整理大全
- 脚本控制:替换、另存为、问候等
- Linux中Ctrl+C、Ctrl+D等按键操作&进程相关命令
- Android中Context详解
- 计算机视觉目标检测的框架与过程
- HDU - 4681 String
- 以水仙花数论C++中while和if的区别
- iOS应用崩溃日志揭秘
- LINUX命令记录(个人)
- Facebook首席运营官桑德伯格在2012哈佛商学院毕业典礼上的演讲
- android学习笔记(5)-SharedPreferences方式保存用户喜好参数(附pull解析Xml文档详解)
- 安装SQL 2000出现问题