博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
getContext(),getApplicationContext(),getBaseContext()和“ this”之间的区别
阅读量:2289 次
发布时间:2019-05-09

本文共 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. 如果可能的话,请举一些简单的例子。


#1楼

参考:


#2楼

  • : 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。


#3楼

Context provides information about the Actvity or Application to newly created components. Context为新创建的组件提供有关ActvityApplication信息。

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 由于ActivityContext的子类,因此可以使用this来获取该活动的上下文


#4楼

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() -仅返回上下文视图当前正在运行的活动。


#5楼

From this 从这个

I understood that you should use: 我了解您应该使用:

Try using the context-application instead of a context-activity 尝试使用上下文应用程序代替上下文活动


#6楼

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 : 如果需要,例如,可能需要访问“基本”上下文,例如,检查它是ServiceActivity还是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/

你可能感兴趣的文章
[Java并发编程实战] 第9章 图形用户界面应用程序
查看>>
[Java并发编程实战] 第10章 避免活跃性危险
查看>>
[Java并发编程实战] 第11章 性能与可伸缩性
查看>>
[Java并发编程实战] 第12章 并发程序的测试
查看>>
[Java并发编程实战] 第13章 显式锁
查看>>
[Java并发编程实战] 第14章 构建自定义的同步工具
查看>>
[Java并发编程实战] 第15章 原子变量与非阻塞同步机制
查看>>
[高性能MySQL] 第1章 MySQL架构与历史
查看>>
[面试] 找实习面试时问到的跟项目、分布式和数据库有关的问题
查看>>
[Docker] 使用Docker-compose部署Neo4j
查看>>
[Neo4j] CQL命令
查看>>
[Neo4j] 添加算法插件包
查看>>
[Neo4j] Spring Boot项目访问Neo4j报错
查看>>
[Docker] 两份docker-compose.xml共用一个network
查看>>
数据总线和地址总线的纠葛
查看>>
iar注释快捷键
查看>>
ubuntu16.04 okular中文界面
查看>>
Intel Core系列CPU架构演变
查看>>
leetcode Perfect Squares
查看>>
Windows Mobile 开发书籍介绍
查看>>