本文转自:http://www.eoeandroid.com/thread-171104-1-1.html

原文转自: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。

PS:
Pointers to documentation on this, and guidance about what might break if the wrong one is chosen, would be much appreciated.
如果你有关于这方面的文章的地址,和关于如果选择错误的写法会产生的错误的指导,将不胜感激

android中获取context的多种方法的区别(this,getbascontext(),getApplicationcontext())相关推荐

  1. android中获取context对象

    在Android中获取context对象 方法一: 先在activity中获取context对象: Context context = MainActivity.this; 哪个类要用到此contex ...

  2. android开发获取imei码,Android中获取IMEI码的方法

    我们在开发项目的时候,通常要获取手机的IMEI码,这是开发的必备需求,接下来,爱站技术频道小编就告诉大家Android中获取IMEI码的方法,希望本文的介绍能提高大家的水平. 核心代码:Imei = ...

  3. android 获取对象,在Android中获取LayoutInflater对象的方法

    1.通过系统服务来获得,这是最标准的: LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYO ...

  4. android代码 IMEI,Android_Android中获取IMEI码的方法,核心代码:Imei = ((TelephonyManager) - phpStudy...

    Android中获取IMEI码的方法 核心代码:Imei = ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)) .getDeviceId ...

  5. android apk 的信息,Android中获取apk基本信息

    一 PackageManager可以获得的所有包节点信息: 1,所有节点的基类:PackageItemInfo: 2,PackageInfo:package的全面信息,与AndroidManifest ...

  6. android调用音量方法,android中获取当前音量大小

    android中获取当前音量大小 (2012-11-07 08:40:41) 标签: it 通过程序获取android系统手机的铃声和音量.同样,设置铃声和音量的方法也很简单! 设置音量的方法也很简单 ...

  7. Android中获取网络图片的三种方法

    android中获取网络图片是一件耗时的操作,如果直接获取有可能会出现应用程序无响应(ANR:Application Not Responding)对话框的情况.对于这种情况,一般的方法就是耗时操作用 ...

  8. android获取颜色资源,Android中获取颜色的几种方法

    Android中获取颜色的几种方法: 通过android封装好的Color类中的常量 public static final int BLACK = 0xFF000000; public static ...

  9. android开发中磁场传感器,Android开发获取传感器数据的方法示例【加速度传感器,磁场传感器,光线传感器,方向传感器】...

    本文实例讲述了Android开发获取传感器数据的方法.分享给大家供大家参考,具体如下: package mobile.android.sensor; import java.util.List; im ...

最新文章

  1. 白话红黑树系列之二——红黑树的构建
  2. 认识网页:html + css + JavaScript
  3. 服务器里这么修改404页面,网站404页面怎么做
  4. C#访问MySQL数据库的方法
  5. angular 的配置文件的应用
  6. 2015-03-17 current note creation logic in my task
  7. docker java mysql_Docker 搭建 MySQL 服务
  8. java对象的序列化机制详解
  9. redis bio线程任务队列
  10. 在Linux中使用GoAccess分析Nginx的日志
  11. matlab中erf什么,matlab中的误差函数erf是什么意思?
  12. 如何在电脑端同时登录多个企业微信或微信
  13. 如何自己搭建一个网盘
  14. 转:细数国内市场智能语音开放平台有哪些?
  15. 纽约理工计算机科学怎么样,纽约大学计算机科学在什么学院?_托普仕留学
  16. Nginx中的include
  17. 小微企业怎么实现数字化转型
  18. 2.Oracle深度学习笔记——内存架构之UGA
  19. ESP32设备驱动-TEA5767收音机模块驱动
  20. 华为USB网卡驱动linux,以usb-modeswitch配华为LTE上网卡EC5373在Linux下试图上网的尝试...

热门文章

  1. 谷粒商城-基础篇-环境搭建(P1-P44)
  2. eclipse java1.8_不同版本Eclipse对JDK版本要求
  3. jks与pkcs12格式的keystore互转
  4. MTK 轨迹球(JOG_BALL)
  5. 已配置好环境,在CMD中输入adb命令,还是提示“‘adb‘ 不是内部或外部命令,也不是可运行程序或批处理文件”的解决方法
  6. 九章量子计算机应用,“九章”量子计算机这么牛,到底有什么用? |【经纬低调分享】...
  7. 【算法】二分法多种情况详解
  8. j-4 大炮打蚊子 (10 分)关于最后一个测试点出错及本题的具体思路(以作者思路为例)
  9. Java基于JSP的论坛交流系统
  10. 双硬盘Win10+Ubuntu双系统安装