第一篇:转自:http://hi.baidu.com/lifj_hogan/item/38c0b8812256e15927ebd9fe
http://stackoverflow.com/questions/1026973/android-whats-the-difference-between-the-various-methods-to-get-a-context
In various bits of Android code I've seen:
在大量的android代码中,我看到如下的写法:
public class MyActivity extends Activity {
public void method() {
mContext = this; // since Activity extends Context
mContext = getApplicationContext();
mContext = getBaseContext();
}
}
However I can't find any decent explanation of which is preferable, and under what circumstances which should be used.
不过,我不能找到任何像样的解释:那种写法是最好的,在哪种情况应使用哪种写法。
Pointers to documentation on this, and guidance about what might break if the wrong one is chosen, would be much appreciated.
如果你有关于这方面的文章的地址,和关于如果选择错误的写法会产生的错误的指导,将不胜感激。
I agree that documentation is sparse when it comes to Contexts in Android, but you can piece together a few facts from various sources.
我承认,涉及android中Contexts的文章凤毛麟角,但是,我们还是可以从各种来源中拼凑出东西的。
This blog post on the official Google Android developers blog was written mostly to help address memory leaks, but provides some good information about contexts as well:
这个谷歌官方的Android开发者写,主要是为了帮助解决内存泄漏,的博客文章,同时也给contexts提供了一些良好的信息:
In a regular Android application, you usually have two kinds of Context, Activity and Application.
在一个普通的Android应用程序中,你通常有两种Context,Activity和Application。
Reading the article a little bit further tells about the difference between to the two and why you might want to consider using the application Context (Activity.getApplicaitonContext()) rather than using the Activity context ("this").Basically the Application context is associated with the Applicaiton and will always be the same throughout the life cycle of you app, where as the Activity context is associated with the activity and could possible be destroyed many times as the activity is destroyed during screen orientation changes and such.
如果再深入读下这篇文章,它告诉了我们这两种Context的不同,并且为什么你会考虑使用应用程序Context(Activity.getApplicaitonContext())而不是使用活动Context(即"this")。基本上来说呢,应用程序Context是和应用程序关联的,并且在程序的生命周期内,总是一样的。但是,活动Context是关联在某个活动上的,随着Activity的销毁(比如横竖屏切换等情况),这个Context也会被销毁N多次。
I couldn't find really anything about when to use getBaseContext() other than / from one /(a for a) liner from Dianne Hackborn, one of the Google engineers working on the Android SDK:
我真的没有找到任何关于使用 getBaseContext()的东西,除了Dianne Hackborn(一位编写Android SDK的谷歌工程师)的文章中的一句话:
Don't use getBaseContext(), just use the Context you have.
不要使用getBaseContext(),只使用你有的Context。
That was from a post on the android-developers newsgroup, you may want to consider asking your question there as well because a handful of the people working on Android actual monitor that newsgroup and answer questions.
那是来自 android-developers 新闻组的一篇邮件。你也可以考虑在那里问你的问题,因为确实有一大帮搞Android的人在管理这个小组,并且回答问题。
So overall it seems preferable to use the global application context when possible.
所以,总体来说,似乎最好在可能的情况下使用全局应用程序Context。
I got bad window token errors when using application context to Progress Dialog. So i used activity context instead.
我使用应用程序Context给进度条对话框的时候,出现了“window token errors”的错误。所以,我使用了活动Context,而不是应用程序Context。
第二篇:转自:http://blog.csdn.net/janronehoo/article/details/7348566
在android中常常会遇到与context有关的内容
浅论一下context : 在语句 AlertDialog.Builder builder = new AlertDialog.Builder(this); 中,要求传递的 参数就是一个context,在这里我们传入的是this,那么这个this究竟指的是什么东东呢? 这里的this指的是Activity.this,是这个语句所在的Activity的this,是这个Activity 的上下文。网上有很多朋友在这里传入this.getApplicationContext(),这是不对的。 AlertDialog对象是依赖于一个View的,而View是和一个Activity对应的。 于是,这里涉及到一个生命周期的问题,this.getApplicationContext()取的是这个应 用程序的Context,Activity.this取的是这个Activity的Context,这两者的生命周期是不同 的,前者的生命周期是整个应用,后者的生命周期只是它所在的Activity。而AlertDialog应 该是属于一个Activity的,在Activity销毁的时候它也就销毁了,不会再存在;但是,如果传 入this.getApplicationContext(),就表示它的生命周期是整个应用程序,这显然超过了它 的生命周期了。 所以,在这里我们只能使用Activity的this。
new AlertDialog.Builder(getApplicationContext())时发生错误:
E/AndroidRuntime(5844): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
于是查了查:
getApplicationContext() 生命周期是整个应用,应用摧毁它才摧毁 Activity.this的context属于activity ,activity 摧毁他就摧毁
activity.this要返回一个activity,而getApplicationContext()就不一定返回一个activity
getApplicationContext() 返回应用的上下文,生命周期是整个应用,应用摧毁它才摧毁
Activity.this的context 返回当前activity的上下文,属于activity ,activity 摧毁他就摧毁
getBaseContext()