android - mainactivity - this 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,它提供訪問應用程序的私有數據,如資源,數據庫,私有文件夾,首選項,設置...

Most of the privatedata are the same for all activities/services/broadcastlisteners of one application.

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應用程序有活動。 上下文就像您的應用程序當前正在運行的環境的句柄。活動對象繼承了上下文對象。

有關更多信息,請參閱使用Android Studio進行Android開發簡介 - 教程


上下文是類android.content.Context的實例提供到執行應用程序的Android系統的連接。 例如,您可以通過上下文檢查當前設備顯示的大小。

它還可以訪問項目的資源。 它是有關應用程序環境的全局信息的接口。

Context類還提供對Android服務的訪問,例如,警報管理器觸發基於時間的事件。

活動和服務擴展了Context類。 因此他們可以直接用於訪問上下文。


可以將其視為運行應用程序或服務的虛擬機。獨立環境可以訪問大量底層系統信息和某些允許的資源。 你需要這種背景才能獲得這些服務。


如果你想在Android中連接Context和其他熟悉的類,請記住這個結構:

上下文<ContextWrapper <應用程序

上下文<ContextWrapper <ContextThemeWrapper <Activity

上下文<ContextWrapper <ContextThemeWrapper <Activity <ListActivity

上下文<ContextWrapper <服務

上下文<ContextWrapper <服務<IntentService

所以,所有這些類都是以他們自己的方式進行的。 如果您願意,您可以將ServiceListActivity強制轉換上下文 。 但是如果仔細觀察,一些類也會繼承主題。 在活動或片段中,您希望將主題應用於您的視圖,但不要關心它的服務類。

我在here解釋不同的背景。


簡單來說:

顧名思義,它是應用程序/對象當前狀態的上下文。 它可以讓新創建的對象了解正在發生的事情。 通常,您可以通過它來獲取有關程序的另一部分(活動和程序包/應用程序)的信息。

您可以通過調用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中的context

每個老闆都有一位助理照顧,做所有不那麼重要和耗時的工作。 如果需要一個文件或一杯咖啡,助理正在運行。 有些老闆幾乎不知道辦公室裡發生了什麼,所以他們也會問他們的助手。 他們自己做一些工作,但對於大多數其他事情他們需要助手的幫助。

在這種情況下,

老闆 - 是Android應用程序

助理 - 是上下文

文件/咖啡 - 是資源

當我們需要獲取有關應用程序的不同部分的信息時,我們通常會調用上下文,如“活動”

涉及上下文的一些操作(需要助理的事情):

加載公共資源創建動態視圖顯示Toast消息啟動活動等獲取上下文的不同方式:

getContext()

getBaseContext()

getApplicationContext()

this

android.content.Context提供到Android系統和項目資源的連接。 它是有關應用程序環境的全局信息的接口。

Context還提供對Android服務的訪問,例如定位服務。

活動和服務擴展了Context類。


類android.content.Context的實例提供到執行應用程序的Android系統的連接。 例如,您可以通過上下文檢查當前設備顯示的大小。

它還可以訪問項目的資源。 它是有關應用程序環境的全局信息的接口。

Context類還提供對Android服務的訪問,例如,警報管理器觸發基於時間的事件。

活動和服務擴展了Context類。 因此他們可以直接用於訪問上下文。





android-context