首先,要搞清楚的是Android中获取上下文的方式:
- this 或者 getContext()
- getApplicationContext()
- getBaseContext()
- getApplication()(严格来说,这个方法不能得到程序的Context)
第一种:this or getContext():
谷歌文档给出的解释如下:
Returns the context the view is running in, through which it can access the current theme, resources, etc.
意思是:返回当前Activity的context,通过它可以得到当前的theme,resources等资源。
特点:属于当前Activity,随着当前Activity的销毁而销毁
第二种: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 int