本文共 4858 字,大约阅读时间需要 16 分钟。
本文翻译自:
What is the difference between getContext()
, getApplicationContext()
, getBaseContext()
, and " this
"? getContext()
, getApplicationContext()
, getBaseContext()
和“ this
”之间有什么区别?
Though this is simple question I am unable to understand the basic difference between them. 尽管这是一个简单的问题,但我无法理解它们之间的基本区别。 Please give some easy examples if possible. 如果可能的话,请举一些简单的例子。
参考:
: Returns the context the view is currently running in. Usually the currently active Activity. :返回视图当前正在其中运行的上下文。通常是当前活动的Activity。
: Returns the context for the entire application (the process all the Activities are running inside of). :返回整个应用程序的上下文(所有Activity在其中运行的进程)。 Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity. 如果您需要一个与整个应用程序的生命周期相关联的上下文,而不仅仅是当前Activity,请使用此上下文代替当前Activity上下文。
: If you need access to a Context from within another context, you use a ContextWrapper. :如果需要从另一个上下文中访问上下文,则可以使用ContextWrapper。 The Context referred to from inside that ContextWrapper is accessed via getBaseContext(). 可以通过getBaseContext()访问从ContextWrapper内部引用的Context。
Context
provides information about the Actvity
or Application
to newly created components. Context
为新创建的组件提供有关Actvity
或Application
信息。
Relevant Context
should be provided to newly created components (whether application context or activity context) 应为新创建的组件提供相关Context
(应用程序上下文还是活动上下文)
Since Activity
is a subclass of Context
, one can use this
to get that activity's context 由于Activity
是Context
的子类,因此可以使用this
来获取该活动的上下文
getApplicationContext() - Returns the context for all activities running in application. getApplicationContext() -返回在应用程序中运行的所有活动的上下文。
getBaseContext() - If you want to access Context from another context within application you can access. getBaseContext() -如果要从应用程序中的另一个上下文访问上下文,则可以访问。
getContext() - Returns the context view only current running activity. getContext() -仅返回上下文视图当前正在运行的活动。
From this 从这个
I understood that you should use: 我了解您应该使用:
Try using the context-application instead of a context-activity 尝试使用上下文应用程序代替上下文活动
Most answers already cover getContext()
and getApplicationContext()
but getBaseContext() is rarely explained. 大多数答案已经涵盖了getContext()
和getApplicationContext()
但很少解释getBaseContext() 。
The method getBaseContext()
is only relevant when you have a ContextWrapper
. 方法getBaseContext()
仅在具有ContextWrapper
时才相关。 Android provides a ContextWrapper
class that is created around an existing Context
using: Android提供了一个ContextWrapper
类,该类是使用以下内容围绕现有Context
创建的:
ContextWrapper wrapper = new ContextWrapper(context);
The benefit of using a ContextWrapper
is that it lets you “modify behavior without changing the original Context”. 使用ContextWrapper
的好处是它使您“无需更改原始Context即可修改行为”。 For example, if you have an activity called myActivity
then can create a View
with a different theme than myActivity
: 例如,如果您有一个名为myActivity
的活动,则可以创建一个主题与myActivity
不同的View
:
ContextWrapper customTheme = new ContextWrapper(myActivity) { @Override public Resources.Theme getTheme() { return someTheme; }}View myView = new MyView(customTheme);
ContextWrapper
is really powerful because it lets you override most functions provided by Context
including code to access resources (eg openFileInput()
, getString()
), interact with other components (eg sendBroadcast()
, registerReceiver()
), requests permissions (eg checkCallingOrSelfPermission()
) and resolving file system locations (eg getFilesDir()
). ContextWrapper
非常强大,因为它使您可以覆盖Context
提供的大多数功能,包括访问资源的代码(例如openFileInput()
, getString()
),与其他组件进行交互(例如sendBroadcast()
, registerReceiver()
),请求权限(例如checkCallingOrSelfPermission()
)和解析文件系统位置(例如getFilesDir()
)。 ContextWrapper
is really useful to work around device/version specific problems or to apply one-off customizations to components such as Views that require a context. ContextWrapper
对于解决特定于设备/版本的问题或将一次性定制应用于需要上下文的组件(例如视图)非常有用。
The method getBaseContext() can be used to access the “base” Context that the ContextWrapper
wraps around. 方法getBaseContext()可用于访问ContextWrapper
环绕的“基本” Context。 You might need to access the “base” context if you need to, for example, check whether it's a Service
, Activity
or Application
: 如果需要,例如,可能需要访问“基本”上下文,例如,检查它是Service
, Activity
还是Application
:
public class CustomToast { public void makeText(Context context, int resId, int duration) { while (context instanceof ContextWrapper) { context = context.baseContext(); } if (context instanceof Service)) { throw new RuntimeException("Cannot call this from a service"); } ... }}
Or if you need to call the “unwrapped” version of a method: 或者,如果您需要调用方法的“未包装”版本:
class MyCustomWrapper extends ContextWrapper { @Override public Drawable getWallpaper() { if (BuildInfo.DEBUG) { return mDebugBackground; } else { return getBaseContext().getWallpaper(); } }}
转载地址:http://eicnb.baihongyu.com/