1.首先什么是context?
文档语焉不详,说是resource什么的,反正是没看懂,实际上可以认为它是一个指向parent对象的指针,受到那个parent对象的控制。
2.为什么需要context?
试想一下这个语句:
Button
这个this就是context,前面说了它指向了parent对象,那指向了哪里?为什么需要这么做?原因是Button为了能完成自己的使命(响应各种操作)就得让Android系统知道自己是属于哪个Activity的,这个信息是必须的,因为只有这样Android系统才会对其进行管理,比如响应onClick()事件,否则系统连Button是属于哪个Activity的都不知道,怎么响应呢?如果Button的context和Activity的context是一样的,那么它们都是可见的,比如上面那个button跟Activity一样是可见的,它们的context指向另一个不可见的对象,也就是它们受那个不可见的对象控制,我理解为系统。
3.Application
这是两种不同的context,也是最常见的两种。第一种中context的生命周期与Application的生命周期相关的,context随着Application的销毁而销毁,第二种中的context跟Activity的生命周期是相关的,但是对一个Application来说,Activity可以销毁几次,那么属于Activity的context就会销毁多次。至于用哪种context,得看应用场景,个人感觉用Activity的context好一点,不过也有的时候必须使用Application的context。
举例来说:SQLite建立时是需要context的,那么我们怎么传呢?
一、建立SQLiteOpenHelper
public
..........
public
int
super(context,
//
}
.......
}
二、我们用另一个类封装一下这个类,完善数据查询、插入、删除、更新等操作,具体方法不表。
public
//定义上面那个类对象;
private
//注意这里得定义一个context,你想直接得到是得不到的,需要调用这个类的对象自己传一个context过来,这个context再将得到的context传到mySQLiteHelper
private
//构造方法
public
this.context
dbHelper
db
}
}
public
private
........
//两种方法都可以,第一种是Application的context,第二种是Activity的context
.........
}
}
在android中context可以作很多操作,但是最主要的功能是加载和访问资源。在android中有两种context,一种是
比如一个activity的onCreate:
protected
}
把activity
这样如果context发生内存泄露的话,就会泄露很多内存。
这里泄露的意思是gc没有办法回收activity的内存。
Leaking
当屏幕旋转的时候,系统会销毁当前的activity,保存状态信息,再创建一个新的。
比如我们写了一个应用程序,它需要加载一个很大的图片,我们不希望每次旋转屏
实现类似:
public
}
这段程序看起来很简单,但是却问题很大。当屏幕旋转的时候会有leak(即gc没法销毁activity)。
我们刚才说过,屏幕旋转的时候系统会销毁当前的activity。但是当drawable和view关联后,drawable保存了view的
避免这种内存泄露的方法是避免activity中的任何对象的生命周期长过activity,避免由于对象对
避免context相关的内存泄露,记住以下几点:
1.
2.
3.
Context字面意思是上下文,位于framework
如下图所示:
Context提供了关于应用环境全局信息的接口。它是一个抽象类,它的执行被Android系统所提供。它允许获取以应用为特征的资源和类型。同时启动应用级的操作,如启动Activity,broadcasting和接收intents。
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很有用。