Android 中 getContext()、getApplicationContext()、getBaseContext() 和 this 的區別?


什麼是 Android 中的 Context?

Context 為我們提供了有關應用程式當前狀態的資訊。它允許我們與 Android 元件進行互動。它允許我們訪問檔案和其他資源,例如圖片、Activity、Fragment 和 Service。在 Android 中,getContext()、getApplicationContext()、getBaseContext()、this 是檢索不同型別 Context 物件的方法。Context 類提供對構建 Android 應用程式所需的多種資源和服務的訪問,包括 -

  • 資源 - 透過 Context 訪問儲存在資源物件中的資源,例如佈局、影像和字串。

  • 系統服務 - 它提供對系統服務的訪問,例如電源管理器、通知管理器和位置管理器。

  • 偏好設定 - 偏好設定用於儲存和檢索鍵值對中的資料,可以透過 Context 訪問。

  • 資產管理器 - 它用於訪問與應用程式打包在一起的原始資產檔案。

  • 資料庫 - 它提供對應用程式使用的資料庫的訪問。

Context 有哪兩種型別?

Context 有兩種型別,如下所示 -

  • Activity Context

  • Application Context

在 Android 中,“Activity” 和“Application” 都是“Context” 類的子類,它提供對特定於應用程式的資源和類的訪問。

這兩個上下文的主要區別在於,“Activity” 上下文與“Activity” 的生命週期相關聯,而“Application” 上下文與整個應用程式的生命週期相關聯。

什麼是 Android 中的 getContext()?

getContext() 方法通常與 Activity 和 Fragment 一起使用,以檢索檢視的上下文。Context 表示應用程式或 Activity 的當前狀態,它提供對各種資源的訪問,例如字串、影像等等。getContext() 可以從任何 View 中呼叫,包括 Button 檢視、Text 檢視或 Image 檢視。

以下是在 Android 應用程式中使用 getContext() 的方法

 public class CustomView extends View {
   public CustomView(Context context) {
       super(context);
   }
   public void displayToastMessage() {
       // Get the context associated with this view
       Context context = getContext();
       // Use the context to access resources from string.xml file.
       String message = context.getString(R.string.my_message);
       // Using the context to display the toast message.
       Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
   }
}

什麼是 Android 中的 getApplicationContext()?

getApplicationContext() 是 Context 類提供的方法。Context 表示物件的當前狀態。getApplicationContext() 可以從任何Activity、Service、BroadcastReceiverContentProvider 中呼叫以檢索全域性應用程式 Context。此方法可以在應用程式的多個元件中安全地使用。

以下是在 Android 應用程式中使用 getApplicationContext() 的方法

public class MyApp extends Application {
   @Override
   public void onCreate() {
       super.onCreate();
       // Get the global application context
       Context context = getApplicationContext();
       // Use the context to access strings.xml file.
       String message = context.getString(R.string.my_message);
       //Using the context to display the toast message on below line.
       Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
   }
}

什麼是 Android 中的 getBaseContext()?

getBaseContext() 是 ContextWrapper 類提供的方法,它返回當前上下文的基上下文。getBaseContext() 可以從任何Activity、Service、BroadcastReceiverContentProvider 中呼叫以檢索全域性應用程式 Context。該方法返回一個 Context 物件,可用於訪問應用程式中的資源或服務。

以下是在 Android 應用程式中使用 getBaseContext() 的方法

getBaseContext() 返回的 Context 用作 Activity 或 Service 的基上下文。您可以安全地使用它來訪問與 Activity 和 Service 關聯的資源或服務。

public class MyApp extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       // Get the base context of this activity on below line.
       Context context = getBaseContext();
       // Use the context to get data from strnig.xml file
       String message = context.getString(R.string.my_message);
       // using the context to display toast message on below line.
       Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
   }
}

什麼是 Android 中的 this?

this 是 Android 中的一個關鍵字,用於獲取我們當前所在的類的上下文。無需獲取上下文,我們可以簡單地使用此關鍵字,它將提供該類的上下文。

以下是在 Android 應用程式中使用 this 的方法

在此示例中,我們不是獲取儲存在變數中的上下文,而是簡單地將其作為 this 傳遞以顯示吐司訊息。

  public class MyApp extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
      Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
   }
}

getContext()、getApplicationContext()、getBaseContext() 和 this 的區別

getApplicationContext()

getBaseContext()

getContext()

this

定義

getApplicationContext() 返回應用程式的全域性上下文。

getBaseContext() 返回整個應用程式的上下文。

getContext() 返回當前 Activity 的上下文。

this 用於引用當前 Activity。

父級

應用程式

應用程式

Activity

Activity

在服務外部可訪問

在內容提供程式外部可訪問

在 Activity 外部可訪問

結論

總而言之,getContext() 返回當前 View 或 Fragment 的 Context,getApplicationContext() 返回應用程式的 Context,getBaseContext() 返回 ContextWrapper 的 Context,而 this 返回當前類的上下文。根據您正在工作的上下文選擇正確的方法非常重要,因為使用錯誤的方法會導致記憶體洩漏或其他問題。

更新於: 2023年5月8日

2K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.