android - getcontext - 安卓context是什么




Android上的“上下文”是什么? (20)

只要你反思,想一想。

与应用程序环境的全局信息的接口。 这是一个抽象类,其实现由Android系统提供。

它允许访问特定于应用程序的资源和类,以及对诸如启动活动,广播和接收意图等应用程序级操作的上调。

在Android编程中, Context类是什么,它用于什么?

我在开发者网站上阅读了它,但我无法清楚地理解它。


Boss Assistant Analogy

Lets have a small analogy before diving deep in the technicality of Context

Every Boss has an assistant or someone( errand boy) who does less important and more time consuming things for him. For example if they need a file or coffee then an assistant will be on run. Boss will not know what is going on in the background but the file or the task will be delivered

So Here
Boss - Android Application
Assistant - Context
File or cup of coffee - Resource

What official Android Developper site says about Context

Context is your accesss point for application related resources

Lets see some of such resources or tasks

  • Launching an activity.

  • Getting absolute path to the application specific cache directory on the filesystem.

  • Determining whether the given permission is allowed for a particular process and user ID running in the system.

  • Checking whether you have been granted a particular permission.

等等。
So if Android application wants to start an activity, it goes straight to Context (Access Point), and the Context class gives him back the resources(Intent in this case).

Like any other class Context class too has fields and methods.
You can explore more about Context in official documentation, it covers pretty much everything, available methods, fields and even how to use fields with methods.


Android Context是一个Interface ,允许访问特定于应用程序的资源和类以及有关应用程序环境的信息。

如果你的android应用程序是一个web应用程序,你的上下文将类似于ServletContext(我没有在这里做一个确切的比较)

您的活动和服务还将Context扩展到它们继承所有这些方法以访问运行该应用程序的环境信息。


Context是每个应用程序的Sandbox特定于Android的API,它提供访问应用程序的私有数据,如资源,数据库,私有文件夹,首选项,设置...

大部分私有数据对于一个应用程序的所有活动/服务/广播听众来说都是相同的。

Since Application, Activity, Service implement the Context interface they can be used where an api call needs a Context parameter


Context is context of current state of the application/object. Its an entity that represents various environment data. Context helps the current activity to interact with out side Android Environment like local files, databases, class loaders associated to the environment, services including system-level services, and more.

A Context is a handle to the system. It provides services like resolving resources, obtaining access to databases and preferences, and so on. An android app has activities. It's like a handle to the environment your application is currently running in. The activity object inherits the Context object. Examples for uses of context :

  1. Creating New objects: Creating new views, adapters, listeners

    TextView tv = new TextView(getContext());
    ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);
    
  2. Accessing Standard Common Resources: Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:

    context.getSystemService(LAYOUT_INFLATER_SERVICE) 
    getApplicationContext().getSharedPreferences(*name*, *mode*);
    
  3. Accessing Components Implicitly: Regarding content providers, broadcasts, intent

    getApplicationContext().getContentResolver().query(uri, ...);
    

Difference between Activity Context and Application Context :

They are both instances of Context , but the application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an Activity. Thus, they have access to different information about the application environment.

If you read the docs at getApplicationContext it notes that you should only use this if you need a context whose lifecycle is separate from the current context.

But in general, use the activity context unless you have a good reason not to.

Need of Context :

The documentation says that every view needs the context to access the right resources (eg the theme, strings etc.).

But why in the constructor and not through setContentView(View)?

1.Because the resources must be accessible while the view is being constructed (the constructor will need some resources to fully initialise the view).

2.This allows the flexibility of using a context that is different from the one of the current activity (imagine a view that uses some other string resources and not the ones from the current activity).

3.The designers of the Android SDK seem to have chosen that the context must be set only once and then stay the same throughout the lifetime of the view. Why context is not determined automatically at construction point?

1.Because there exists no static variable that would tell you the current global context of your application. The method getApplicationContext() is the closest to this, but it's not static, so you need an instance of the Activity object to call it.

2.The Java language provides an option to look through the call stack and find whether the View has been constructed in a Context class. But what if there are many? Or what if there are none? This method is very expensive and error prone. So the designers of the API decided that a context must be manually provided.


Context means component (or application) in various time-period. If I do eat so many food between 1 to 2 pm then my context of that time is used to access all methods (or resources) that I use during that time. Content is an component (application) for particular time. Context of components of application keeps changing based on the underlying lifecycle of the components or application. For instance, inside the onCreate() of an Activity ,

getBaseContext() -- gives the context of the Activity that is set (created) by the constructor of activity. getApplicationContext() -- gives the Context setup (created) during the creation of application.

Note: <application> holds all Android Components.

<application>
    <activity> .. </activity> 

    <service>  .. </service>

    <receiver> .. </receiver>

    <provider> .. </provider>
</application> 

It means, when you call getApplicationContext() from inside whatever component, you are calling the common context of the whole application.

Context keeps being modified by the system based on the lifecycle of components.


Context就是我们大多数人所称的应用程序 。 它是由Android系统制作的,并且只能执行应用程序的功能。 在Tomcat中,Context也是我所说的应用程序。

有一个持有许多活动的上下文,每个活动可能有许多视图。

显然,有些人会说它不适合这个或那个,他们可能是正确的,但是说一个Context是你当前的应用程序将帮助你理解你在方法参数中的内容。


ANDROID AND CONTEXT如果您仔细查看各种Android API,您会注意到其中许多人将android.content.Context对象作为参数。 你还会看到一个Activity或者一个服务通常被用作一个Context。 这是可行的,因为这两个类都是从Context开始的。

什么是上下文? 根据Android参考文档,它是一个表示各种环境数据的实体。 它提供对本地文件,数据库,与环境相关的类加载器的访问,包括系统级服务等服务。 在本书的整个过程中,在您使用Android进行日常编码时,您会看到经常传递的上下文。 来自:“Android实践”书。


Context in Android is an interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.


In Java, we say this keyword refers to the state of the current object of the application.
Similarly, in an alternate we have Context in Android Development.

This can be defined either explicitly or implicitly,

Context con = this;

getApplicationContext();

getBaseContext();

getContext();

上下文基本上用于资源访问和获取应用程序(用于应用程序上下文)或活动(用于活动上下文)或任何其他的环境细节...

为了避免内存泄漏,您应该为需要上下文对象的每个组件使用应用程序上下文....更多信息,请点击here


上下文是对当前对象的引用。 此外,上下文允许访问有关应用程序环境的信息。


上下文是类android.content.Context的实例提供到执行应用程序的Android系统的连接。 例如,您可以通过上下文检查当前设备显示的大小。

它还可以访问项目的资源。 它是有关应用程序环境的全局信息的接口。

Context类还提供对Android服务的访问,例如,警报管理器触发基于时间的事件。

活动和服务扩展了Context类。 因此他们可以直接用于访问上下文。


上下文是系统的句柄; 它提供的服务包括解析资源,获取数据库和首选项等等。 一个Android应用程序有活动。 上下文就像您的应用程序当前正在运行的环境的句柄。活动对象继承了上下文对象。

有关更多信息,请参阅使用Android Studio进行Android开发简介 - 教程


只是把它放在那里为新手;

所以先了解Word上下文:

在英文中。 它的意思是:

“构成事件,陈述或想法的环境,以及可以充分理解和评估的环境。”

“书面或口语中的某些部分立即在一个词或一段经文之后,并澄清其含义。”

现在对编程世界有同样的理解:

应用程序/对象的当前状态的上下文。 它可以让新创建的对象了解到底发生了什么。 通常,您可以通过它来获取有关程序另一部分的信息(活动,程序包/应用程序)

您可以通过调用getApplicationContext()getContext(), getBaseContext()this (在activity类中)来获取上下文。

要在应用程序中的任意位置使用以下代码:

在android应用程序中创建新的类AppContext

public class AppContext extends Application {

    private static Context context;

    public void onCreate(){
        super.onCreate();
        AppContext.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return AppContext.context;
    }
}

现在,无论何时您希望在非活动类中都可以使用应用程序上下文,请调用此方法并且您拥有应用程序上下文

希望这个帮助;)


可以将其视为运行应用程序或服务的虚拟机。独立环境可以访问大量底层系统信息和某些允许的资源。 你需要这种背景才能获得这些服务。


把简单的Androids Context弄得一塌糊涂,除非你不再担心,否则你不会爱上它。

Android Context是:

  • 神的对象。

  • 当你开始为Android开发时,你想要传递所有的应用程序,但是当你接近编程,测试和Android本身时,会避免这样做。

    • 不明确的依赖。

    • 内存泄漏的常见原因。

    • PITA进行测试。

  • Android系统使用的实际上下文来分派权限,资源,偏好,服务,广播,样式,显示对话框和充气布局。 而且你需要不同的Context实例来完成一些单独的事情(显然,你不能从应用程序或服务上下文中显示对话框;从应用程序和活动上下文中夸大的布局可能不同)。


简单来说:

顾名思义,它是应用程序/对象当前状态的上下文。 它可以让新创建的对象了解正在发生的事情。 通常,您可以通过它来获取有关程序的另一部分(活动和包/应用程序)的信息。

您可以通过调用getApplicationContext()getContext()getBaseContext()this (在从Context扩展的类中,如Application,Activity,Service和IntentService类)来获取Context

上下文的典型用途:

  • 创建新对象 :创建新视图,适配器,侦听器:

    TextView tv = new TextView(getContext());
    ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);
    
  • 访问标准的公共资源 :LAYOUT_INFLATER_SERVICE,SharedPreferences之类的服务:

    context.getSystemService(LAYOUT_INFLATER_SERVICE)
    getApplicationContext().getSharedPreferences(*name*, *mode*);
    
  • 隐式访问组件 :关于内容提供者,广播,意图

    getApplicationContext().getContentResolver().query(uri, ...);
    

android.content.Context提供到Android系统和项目资源的连接。 它是有关应用程序环境的全局信息的接口。

Context还提供对Android服务的访问,例如定位服务。

活动和服务扩展了Context类。


类android.content.Context的实例提供到执行应用程序的Android系统的连接。 例如,您可以通过上下文检查当前设备显示的大小。

它还可以访问项目的资源。 它是有关应用程序环境的全局信息的接口。

Context类还提供对Android服务的访问,例如,警报管理器触发基于时间的事件。

活动和服务扩展了Context类。 因此他们可以直接用于访问上下文。







android-context