对于context的理解
2013-05-13 17:40
351 查看
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以及BroadcastReceiver。
Context提供了关于应用环境全局信息的接口。它是一个抽象类,它的执行被Android系统所提供。它允许获取以应用为特征的资源和类型。同时启动应用级的操作,如启动Activity,broadcasting和接收intents。
在android中context可以作很多操作,但是最主要的功能是加载和访问资源。在android中有两种context,一种是 application context,一种是activity context,通常我们在各种类和方法间传递的是activity context。
把activity context传递给view,意味着view拥有一个指向activity的引用,进而引用activity占有的资源:view hierachy, resource等。
当屏幕旋转的时候,系统会销毁当前的activity,保存状态信息,再创建一个新的。比如我们写了一个应用程序,它需要加载一个很大的图片,我们不希望每次旋转屏幕的时候都销毁这个图片,重新加载。实现这个要求的简单想法就是定义一个静态的Drawable,这样Activity 类创建销毁它始终保存在内存中。
上面这段程序看起来很简单,但是却问题很大。当屏幕旋转的时候会有leak(即gc没法销毁activity)。我们刚才说过,屏幕旋转的时候系统会销毁当前的activity。但是当drawable和view关联后,drawable保存了view的 reference,即sBackground保存了label的引用,而label保存了activity的引用。既然drawable不能销毁,它所引用和间接引用的都不能销毁,这样系统就没有办法销毁当前的activity,于是造成了内存泄露。gc对这种类型的内存泄露是无能为力的。避免这种内存泄露的方法是避免activity中的任何对象的生命周期长过activity,避免由于对象对
activity的引用导致activity不能正常被销毁。
了防止内存泄露,我们应该注意以下几点:
不要让生命周期长的对象引用activity context,即保证引用activity的对象要与activity本身生命周期是一样的
对于生命周期长的对象,可以使用application context
避免非静态的内部类,尽量使用静态类,避免生命周期问题,注意内部类对外部对象引用导致的生命周期变化
application
context
我们可以使用application context。application context伴随application的一生,与activity的生命周期无关。application
context可以通过Context.getApplicationContext或者Activity.getApplication方法获取。
Java里面通常是用一个static的变量(例如singleton之类的)来同步activity之间(程序里面类之间)的状态。在android里面比较靠谱的做法是用application
context来关联这些状态。
每个activity都是context,里面包含了运行时的状态。同样application也有一个context,android会保证这个context是唯一的实例。
做一个你自己的application context需要继承android.app.Application,然后在app的manifest里面说明这个类。android会自动帮你创建你这个类的实例,接着你用Context.getApplicationContext()方法就能在各个activity里
面获得这个application
context了。
下面介绍Context的一些get方法,通过这些get方法可以获取应用环境全局信息:
Return the context of the single, global Application object of the current process.
Return the full application info for this context's package.
Return a ContentResolver instance for your application's package.
Return PackageManager instance to find global package information.
Return the name of this application's package.
Return a Resources instance for your application's package.
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.
Return a localized string from the application's package's default string table.
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:
摘自: http://www.cnblogs.com/smalltigerlee/archive/2011/11/10/2244324.html
Context才能识别调用者的实例,比如说Toast的第一个参数就是Context,一般在Activity中我们直接用this代替,代表调用者的 实例为Activity,而到了一个button的onClick(View view)等方法时,我们用this时就会报错,所以我们可能使用ActivityName.this来解决,主要原因是因为实现Context的类主要有Android特有的几个模型,Activity、Service以及BroadcastReceiver。
Context提供了关于应用环境全局信息的接口。它是一个抽象类,它的执行被Android系统所提供。它允许获取以应用为特征的资源和类型。同时启动应用级的操作,如启动Activity,broadcasting和接收intents。
在android中context可以作很多操作,但是最主要的功能是加载和访问资源。在android中有两种context,一种是 application context,一种是activity context,通常我们在各种类和方法间传递的是activity context。
把activity context传递给view,意味着view拥有一个指向activity的引用,进而引用activity占有的资源:view hierachy, resource等。
当屏幕旋转的时候,系统会销毁当前的activity,保存状态信息,再创建一个新的。比如我们写了一个应用程序,它需要加载一个很大的图片,我们不希望每次旋转屏幕的时候都销毁这个图片,重新加载。实现这个要求的简单想法就是定义一个静态的Drawable,这样Activity 类创建销毁它始终保存在内存中。
public class myactivity extends Activity { private static Drawable sBackground; protected void onCreate(Bundle state) { super.onCreate(state); TextView label = new TextView(this); label.setText("Leaks are bad"); if (sBackground == null) { sBackground = getDrawable(R.drawable.large_bitmap); } label.setBackgroundDrawable(sBackground);//drawable attached to a view setContentView(label); } }
上面这段程序看起来很简单,但是却问题很大。当屏幕旋转的时候会有leak(即gc没法销毁activity)。我们刚才说过,屏幕旋转的时候系统会销毁当前的activity。但是当drawable和view关联后,drawable保存了view的 reference,即sBackground保存了label的引用,而label保存了activity的引用。既然drawable不能销毁,它所引用和间接引用的都不能销毁,这样系统就没有办法销毁当前的activity,于是造成了内存泄露。gc对这种类型的内存泄露是无能为力的。避免这种内存泄露的方法是避免activity中的任何对象的生命周期长过activity,避免由于对象对
activity的引用导致activity不能正常被销毁。
了防止内存泄露,我们应该注意以下几点:
不要让生命周期长的对象引用activity context,即保证引用activity的对象要与activity本身生命周期是一样的
对于生命周期长的对象,可以使用application context
避免非静态的内部类,尽量使用静态类,避免生命周期问题,注意内部类对外部对象引用导致的生命周期变化
application
context
我们可以使用application context。application context伴随application的一生,与activity的生命周期无关。application
context可以通过Context.getApplicationContext或者Activity.getApplication方法获取。
Java里面通常是用一个static的变量(例如singleton之类的)来同步activity之间(程序里面类之间)的状态。在android里面比较靠谱的做法是用application
context来关联这些状态。
每个activity都是context,里面包含了运行时的状态。同样application也有一个context,android会保证这个context是唯一的实例。
做一个你自己的application context需要继承android.app.Application,然后在app的manifest里面说明这个类。android会自动帮你创建你这个类的实例,接着你用Context.getApplicationContext()方法就能在各个activity里
面获得这个application
context了。
class MyApp extends Application { private String myState; public String getState(){ return myState; } public void setState(String s){ myState = s; } } class Blah extends Activity { @Override public void onCreate(Bundle b){ ... MyApp appState = ((MyApp)getApplicationContext()); String state = appState.getState(); ... } }
下面介绍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 willsee 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:摘自: http://www.cnblogs.com/smalltigerlee/archive/2011/11/10/2244324.html
相关文章推荐
- Android中对于Context的理解
- 对于SSM中的applicationContext.xml 文件中MapperScannerConfigurer的理解
- 对于Context的理解
- 对于解耦的理解
- 对于用户名存在与否逻辑理解
- Android源码分析-全面理解Context
- 深入理解Javascript之执行上下文(Execution Context)
- 对于Python中if __name__=='__main__' 的理解
- 对于虚析构函数的理解
- word2vec中对于Negative Sampling的理解
- 对于S5PV210加载u-boot过程的理解
- Android应用开发中关于this.context=context的理解
- 对于逆元的理解
- 对于矩阵的理解-- by 孟岩老师
- 我对于准确率---p值,召回率R值以及F值的理解
- context 的理解
- OpenCV中对于cvCreateImage和cvReleaseImage的一些小理解
- 我对于互联网发展的看法和一些理解
- c#打包文件解压缩 C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法 一个简单例子理解C#的协变和逆变 对于过长字符串的大小比对
- 对于linux下system()函数的深度理解(整理)