getApplicationContext()、Activity.this、 getBaseContext区别
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区别的更多相关文章
- android开发中的 Activity 与 Context 区别与联系
Context 是 Application /Activity /Service的基类 Intent(Context , Class); Activity中的上下文Context是随着活动的产生而产生 ...
- context和getApplicationContext()的区别
在android中常常会遇到与context有关的内容 浅论一下 context : 在语句 AlertDialog.Builder builder = new AlertDialog.Builder ...
- activity启动模式区别和优化
初学android的开发人员,可能会经常忽略这个重要的设置. Activity一共有以下四种launchMode:1.standard2.singleTop3.singleTask4.singleIn ...
- 【安卓面试题】在一个Activity启动另一个Activity和在Service中启动一个Activity有什么区别
在Activity中可以直接使用Intent启动另一个Activity 显式Intent intent = new Intent(context, activity.class) 隐式 Intent ...
- FragmentActivity和Activity的具体区别在哪里
fragment是3.0以后的东西,为了在低版本中使用fragment就要用到android-support-v4.jar兼容包,而fragmentActivity就是这个兼容包里面的,它提供了操作f ...
- android ApplicationContext Context Activity 内存的一些学习
Android中context可以作很多操作,但是最主要的功能是加载和访问资源. 在android中有两种context,一种是application context,一种是activity cont ...
- Android 如何利用Activity的Dialog风格完成弹出框设计
在我们使用Dialog时,如果需要用到很多自己设计的控件,虽然可以让弹出框显示出我们需要的界面,但却无法找到地方完成控制代码的编写,如何解决这个问题呢,我们可以将Activity伪装成Dialog弹出 ...
- Activity 的 36 大难点,你会几个?「建议收藏」
前言 学 Android 有一段时间了,一直都只顾着学新的东西,最近发现很多平常用的少的东西竟让都忘了,趁着这两天,打算把有关 Activity 的内容以问题的形式梳理出来,也供大家查缺补漏. 本文中 ...
- Activity 的 36 大难点,你会几个
前言 学 Android 有一段时间了,一直都只顾着学新的东西,最近发现很多平常用的少的东西竟让都忘了,趁着这两天,打算把有关 Activity 的内容以问题的形式梳理出来,也供大家查缺补漏. 本文中 ...
随机推荐
- PHP——数组中的each(),list()和while循环遍历数组
.while遍历 在while()语句每次循环中,each()语句将当前数组元素的键,赋给list()函数的第一个参数变量$key.并将当前数组元素中的值,赋给list()函数中的第二个参数变量$va ...
- 干接点&湿接点
干接点的定义: 无源开关:具有闭合和断开的2种状态:2个接点之间没有极性,可以互换: 常见的干接点信号有: 1.各种开关如:限位开关.行程开关.脚踏开关.旋转开关.温度开关.液位开关等: 2.各种按键 ...
- oozie中事件触发input-events和done-flag
样例如下: <coordinator-app name="test_job" frequency="${coord:days(1)}" start=&qu ...
- AJAX的中文乱码问题
/***********本人原创,欢迎转载,转载请保留本人信息*************/作者:wallimn电邮:wallimn@sohu.com博客:http://blog.csdn.net/wa ...
- html -- contenteditable 属性:指定元素内容是否可编辑
所有主流浏览器都支持 contenteditable 属性 定义和用法 contenteditable 属性指定元素内容是否可编辑. 注意: 当元素中没有设置 contenteditable 属性时, ...
- Myeclipse怎么导入project项目
1,打开Meclipse,在左面的区域点击右键,选择import键. 2,在import面板中选择Exiting Projects into Workbence,点击Next, 3,选择Browse. ...
- TreeSet排序,存储自己定义对象,自己定义比較器演示样例
Set:无序.不能够反复元素. |--HashSet:数据结构是哈希表.线程是非同步的. 保证元素唯一性的原理:推断元素的hashCode值是否同样. 假设同样,还会继续推断元素的equals方法.是 ...
- JBPM4.4_工作流基础_准备jBPM4.4环境
1. 工作流基础 1.1. 工作流相关概念 工作流(Workflow),就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档.信息或任 ...
- ArcGIS -- ArcSDE Lock request conflicts with an established lock
1.查询下列表是否有数据 select * from sde.state_locks; select * from sde.object_locks; select * from sde.layer_ ...
- Linux命令之乐--script和scriptplay
script和scriptplay可以把终端会话记录到一个文件中,可以用来制作命令行教学视屏. 开始录制会话 [root@new test]# script -t >timing.log -a ...