大家好,
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
从上可知一下三点,即:
2、该类是一个抽象(abstract class)类,Android提供了该抽象类的具体实现类(后面我们会讲到是ContextIml类)。
信息 等。。
一、Context相关类的继承关系
相关类介绍:
Context类
说明:
1. 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类
说明:该Context类的实现类为ContextIml,该类实现了Context类的功能。请注意,该函数的大部分功能都是直接调用
1.
2. ContextImpl extends Context{
3. //所有Application程序公用一个mPackageInfo对象
4. ActivityThread.PackageInfo mPackageInfo;
5.
6. @Override
7. public Object getSystemService(String name){
8. ...
9. else if (ACTIVITY_SERVICE.equals(name)) {
10. return getActivityManager();
11. }
12. else if (INPUT_METHOD_SERVICE.equals(name)) {
13. return InputMethodManager.getInstance(this);
14. }
15. }
16. @Override
17. public void startActivity(Intent intent) {
18. ...
19. //开始启动一个Activity
20. mMainThread.getInstrumentation().execStartActivity(
21. getOuterContext(), mMainThread.getApplicationThread(), null, null, intent, -1);
22. }
23. }
ContextWrapper类
说明: 正如其名称一样,该类只是对Context类的一种包装,该类的构造函数包含了一个真正的Context引用,即ContextIml
对象。
1. class ContextWrapper extends Context {
2. Context mBase; //该属性指向一个ContextIml实例,一般在创建Application、Service、Activity时赋值
3.
4. //创建Application、Service、Activity,会调用该方法给mBase属性赋值
5. protected void attachBaseContext(Context base) {
6. if (mBase != null) {
7. throw new IllegalStateException("Base context already set");
8. }
9. mBase = base;
10. }
11. @Override
12. public void startActivity(Intent intent) {
13. mBase.startActivity(intent); //调用mBase实例方法
14. }
15. }
ContextThemeWrapper类
说明:该类内部包含了主题(Theme)相关的接口,即android:theme属性指定的。只有Activity需要主题,Service不需要主题,
1. class ContextThemeWrapper extends ContextWrapper {
2. //该属性指向一个ContextIml实例,一般在创建Application、Service、Activity时赋值
3.
4. private Context mBase;
5. //mBase赋值方式同样有一下两种
6. public ContextThemeWrapper(Context base, int themeres) {
7. super(base);
8. mBase = base;
9. mThemeResource = themeres;
10. }
11.
12. @Override
13. protected void attachBaseContext(Context newBase) {
14. super.attachBaseContext(newBase);
15. mBase = newBase;
16. }
17. }
二、 什么时候创建Context实例
熟悉了Context的继承关系后,我们接下来分析应用程序在什么情况需要创建Context对象的?应用程序创建Context实例的
情况有如下几种情况:
具体创建Context的时机
每个应用程序在第一次启动时,都会首先创建Application对象。如果对应用程序启动一个Activity(startActivity)流程比较
清楚的话,创建Application的时机在创建handleBindApplication()方法中,该函数位于 ActivityThread.java类中 ,如下:
1.
2. final void handleBindApplication(AppBindData data){
3. ...
4. ///创建Application对象
5. Application app = data.info.makeApplication(data.restrictedBackupMode, null);
6. ...
7. }
8.
9. Application makeApplication(boolean forceDefaultAppClass, Instrumentation instrumentation) {
10. ...
11. try {
12. java.lang.ClassLoader cl = getClassLoader();
13. ContextImpl appContext = new ContextImpl(); //创建一个ContextImpl对象实例
14. appContext.init(this, null, mActivityThread); //初始化该ContextIml实例的相关属性
15. ///新建一个Application对象
16. app = mActivityThread.mInstrumentation.newApplication(
17. cl, appClass, appContext);
18. appContext.setOuterContext(app); //将该Application实例传递给该ContextImpl实例
19. }
20. ...
21. }
回调handleLaunchActivity()方法,该方法继而调用performLaunchActivity()方法,去创建一个Activity实例,并且回调
onCreate(),onStart()方法等, 函数都位于 ActivityThread.java类 ,如下:
1.
2. final void handleLaunchActivity(ActivityRecord r, Intent customIntent) {
3. ...
4. Activity a = performLaunchActivity(r, customIntent); //启动一个Activity
5. }
6. final Activity performLaunchActivity(ActivityRecord r, Intent customIntent) {
7. ...
8. Activity activity = null;
9. try {
10. //创建一个Activity对象实例
11. java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
12. activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent);
13. }
14. if (activity != null) {
15. ContextImpl appContext = new ContextImpl(); //创建一个Activity实例
16. appContext.init(r.packageInfo, r.token, this); //初始化该ContextIml实例的相关属性
17. appContext.setOuterContext(activity); //将该Activity信息传递给该ContextImpl实例
18. ...
19. }
20. ...
21. }
3、创建Service对象的时机
通过startService或者bindService时,如果系统检测到需要新创建一个Service实例,就会回调handleCreateService()方法,
完成相关数据操作。handleCreateService()函数位于 ActivityThread.java类,如下:
1.
2. final void handleCreateService(CreateServiceData data){
3. ...
4. //创建一个Service实例
5. Service service = null;
6. try {
7. java.lang.ClassLoader cl = packageInfo.getClassLoader();
8. service = (Service) cl.loadClass(data.info.name).newInstance();
9. } catch (Exception e) {
10. }
11. ...
12. ContextImpl context = new ContextImpl(); //创建一个ContextImpl对象实例
13. context.init(packageInfo, null, this); //初始化该ContextIml实例的相关属性
14. //获得我们之前创建的Application对象信息
15. Application app = packageInfo.makeApplication(false, mInstrumentation);
16. //将该Service信息传递给该ContextImpl实例
17. context.setOuterContext(service);
18. ...
19. }
另外,需要强调一点的是,通过对ContextImp的分析可知,其方法的大多数操作都是直接调用其属性mPackageInfo(该属性类
型为PackageInfo)的相关方法而来。这说明ContextImp是一种轻量级类,而PackageInfo才是真正重量级的类。而一个App里的
所有ContextIml实例,都对应同一个packageInfo对象。
法就是通过调用getSharedPreferences()方法去根据相关信息获取SharedPreferences对象。具体流程如下:
1 、调用
1.
2. static final HashMap<File, SharedPreferencesImpl> sSharedPrefs =
3. new HashMap<File, SharedPreferencesImpl>();
4.
5. @Override
6. SharedPreferences getSharedPreferences(String name, int mode){
7. //其所对应的SharedPreferencesImpl对象 ,该对象已一个HashMap集合保存了我们对该文件序列化结果
8. SharedPreferencesImpl sp;
9. File f = getSharedPrefsFile(name); //该包下是否存在对应的文件,不存在就新建一个
10. synchronized (sSharedPrefs) { //是否已经读取过该文件,是就直接返回该SharedPreferences对象
11. sp = sSharedPrefs.get(f);
12. if (sp != null && !sp.hasFileChanged()) {
13. //Log.i(TAG, "Returning existing prefs " + name + ": " + sp);
14. return sp;
15. }
16. }
17. //以下为序列化该xml文件,同时将数据写到map集合中
18. Map map = null;
19. if (f.exists() && f.canRead()) {
20. try {
21. str = new FileInputStream(f);
22. map = XmlUtils.readMapXml(str);
23. str.close();
24. }
25. ...
26. }
27.
28. synchronized (sSharedPrefs) {
29. if (sp != null) {
30. //Log.i(TAG, "Updating existing prefs " + name + " " + sp + ": " + map);
31. sp.replace(map); //更新数据集合
32. } else {
33. sp = sSharedPrefs.get(f);
34. if (sp == null) {
35. //新建一个SharedPreferencesImpl对象,并且设置其相关属性
36. sp = new SharedPreferencesImpl(f, mode, map);
37. sSharedPrefs.put(f, sp);
38. }
39. }
40. return sp;
41. }
42. }
1. ContextIml时接触过
2.
3. static final class SharedPreferencesImpl implements SharedPreferences{
4. private Map mMap; //保存了该文件序列化结果后的操作, 键值对形式
5.
6. //通过key值获取对应的value值
7. public String getString(String key, String defValue) {
8. synchronized (this) {
9. String v = (String)mMap.get(key);
10. return v != null ? v : defValue;
11. }
12. }
13. ...
14. //获得该SharedPreferencesImpl对象对应的Edito类,对数据进行操作
15. public final class EditorImpl implements Editor {
16. private final Map<String, Object> mModified = Maps.newHashMap(); //保存了对键值变化的集合
17. }
18. }