getApplicationContext()返回应用的上下文,生命周期是整个应用,应用退出它才被摧毁

Activity.this 返回当前activity的上下文,属于activity ,activity destory,它也被摧毁

getBaseContext()  the base context as set by the constructor or setBaseContext

Context

public abstract Context getApplicationContext ()

  Return the context of the single, global Application object of the current process. This generally should only be used if you

need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the

current component.

Consider for example how this interacts with registerReceiver(BroadcastReceiver, IntentFilter):

  • If used from an Activity context, the receiver is being registered within that activity. This means that you are expected to

unregister before the activity is done being destroyed; in fact if you do not do so, the framework will clean up your leaked

registration as it removes the activity and log an error. Thus, if you use the Activity context to register a receiver that is

static (global to the process, not associated with an Activity instance) then that registration will be removed on you at whatever

point the activity you used is destroyed.

  • If used from the Context returned here(getApplicationContext), the receiver is being registered with the global state associated

with your application.Thus it will never be unregistered for you. This is necessary if the receiver is associated with static data, not a

particular component.However using the ApplicationContext elsewhere can easily lead to serious leaks if you forget to unregister, unbind, etc.

  AlertDialog.Builder builder = new AlertDialog.Builder(this)中,要求传递的参数就是一个context,在这里我们传入的是当前Activity。

AlertDialog应该是属于当前Activity的,在Activity销毁的时候它也就销毁了,但是,如果传入this.getApplicationContext(),就表示它的生命周期是整个

应用程序,这显然超过了它的生命周期了。所以,在这里我们只能使用当前Activity

1.dialog = ProgressDialog.show(getBaseContext(), "", "正在刷新好友列表");

出错:Unable to add window -- token null is not for an application

getApplicationContext()、Activity.this、 getBaseContext区别的更多相关文章

  1. android开发中的 Activity 与 Context 区别与联系

    Context 是 Application /Activity /Service的基类 Intent(Context , Class); Activity中的上下文Context是随着活动的产生而产生 ...

  2. context和getApplicationContext()的区别

    在android中常常会遇到与context有关的内容 浅论一下 context : 在语句 AlertDialog.Builder builder = new AlertDialog.Builder ...

  3. activity启动模式区别和优化

    初学android的开发人员,可能会经常忽略这个重要的设置. Activity一共有以下四种launchMode:1.standard2.singleTop3.singleTask4.singleIn ...

  4. 【安卓面试题】在一个Activity启动另一个Activity和在Service中启动一个Activity有什么区别

    在Activity中可以直接使用Intent启动另一个Activity 显式Intent intent = new Intent(context, activity.class) 隐式 Intent ...

  5. FragmentActivity和Activity的具体区别在哪里

    fragment是3.0以后的东西,为了在低版本中使用fragment就要用到android-support-v4.jar兼容包,而fragmentActivity就是这个兼容包里面的,它提供了操作f ...

  6. android ApplicationContext Context Activity 内存的一些学习

    Android中context可以作很多操作,但是最主要的功能是加载和访问资源. 在android中有两种context,一种是application context,一种是activity cont ...

  7. Android 如何利用Activity的Dialog风格完成弹出框设计

    在我们使用Dialog时,如果需要用到很多自己设计的控件,虽然可以让弹出框显示出我们需要的界面,但却无法找到地方完成控制代码的编写,如何解决这个问题呢,我们可以将Activity伪装成Dialog弹出 ...

  8. Activity 的 36 大难点,你会几个?「建议收藏」

    前言 学 Android 有一段时间了,一直都只顾着学新的东西,最近发现很多平常用的少的东西竟让都忘了,趁着这两天,打算把有关 Activity 的内容以问题的形式梳理出来,也供大家查缺补漏. 本文中 ...

  9. Activity 的 36 大难点,你会几个

    前言 学 Android 有一段时间了,一直都只顾着学新的东西,最近发现很多平常用的少的东西竟让都忘了,趁着这两天,打算把有关 Activity 的内容以问题的形式梳理出来,也供大家查缺补漏. 本文中 ...

随机推荐

  1. 亿级日PV的魅族云同步的核心协议与架构实践(转)

    云同步的业务场景 这是魅族云同步的演进,第一张是M8.M9,然后到后面的是MX系统,M9再往后发展,我们的界面可以看到基本上是没有什么变化的,但本质发生了很大的变化,我们经过了一些协议优化,发展到今天 ...

  2. 概率论 - BZOJ - 4001 TJOI2015

    TJOI2015 Problem's Link ---------------------------------------------------------------------------- ...

  3. thinkphp 设置 支持模块多组

    正常的访问地址是:localhost/项目名/index.php/Admin/Index/index 模块分组之后呢:localhost/项目名/index.php/Admin/System/Inde ...

  4. 字符串类为JAVA中的特殊类

    字符串类为JAVA中的特殊类,String中为final类,一个字符串的值不可重复.因此在JAVA VM(虚拟机)中有一个字符串池,专门用来存储字符串.如果遇到String a=”hello”时(注意 ...

  5. 断言(assert)和程序的安全保证

    断言,用来DEBUG错误的,在DEBUG时发现然后跟踪错误! 通常 写一个程序给别人使用的,这个代码在安全性上的要求是什么呢?直觉上,我们都知道程序不应该崩.但是通常C/C++的程序如果把包含API的 ...

  6. 怎么取消环境变量设置 shell

    Linux字符模式下设置/删除环境变量方法: bash下 设置:export 变量名=变量值 删除:unset 变量名 转自:http://zhidao.baidu.com/link?url=sY3u ...

  7. [NOIP 2014复习]第二章:搜索

    一.深度优先搜索(DFS) 1.Wikioi 1066引水入城 题目描写叙述 Description 在一个遥远的国度,一側是风景秀美的湖泊,还有一側则是漫无边际的沙漠.该国的行政 区划十分特殊,刚好 ...

  8. Linux命令之乐--expr

    计算字符长度 [root@Director ~]# echo $var hello world [root@Director test]# expr length "$var" 数 ...

  9. python学习【第一篇】python介绍

    python发展历史 起源 Python的作者,Guido von Rossum,荷兰人.1982年,Guido从阿姆斯特丹大学获得了数学和计算机硕士学位.然而,尽管他算得上是一位数学家,但他更加享受 ...

  10. 【BZOJ1217】[HNOI2003]消防局的设立 树形DP

    [BZOJ1217][HNOI2003]消防局的设立 Description 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地, ...