第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

getContext()、getApplicationContext()、getBaseConte

getContext()、getApplicationContext()、getBaseConte

縹緲止盈 2019-06-18 15:50:38
getContext()、getApplicationContext()、getBaseContext()和“this”之間的區(qū)別.之間的區(qū)別是什么?getContext() , getApplicationContext() , getBaseContext(),和“this"?雖然這是一個(gè)簡(jiǎn)單的問題,但我無法理解它們之間的基本區(qū)別。如果可能的話,請(qǐng)給出一些簡(jiǎn)單的例子。
查看完整描述

3 回答

?
慕俠2389804

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊

  • View.getContext():返回視圖當(dāng)前運(yùn)行的上下文。通常是當(dāng)前活躍的活動(dòng)。

  • Activity.getApplicationContext():返回整個(gè)應(yīng)用程序的上下文(所有活動(dòng)都在其內(nèi)部運(yùn)行的進(jìn)程)。如果您需要一個(gè)與整個(gè)應(yīng)用程序的生命周期相關(guān)聯(lián)的上下文,而不僅僅是當(dāng)前的活動(dòng),請(qǐng)使用它而不是當(dāng)前的活動(dòng)上下文。

  • ContextWrapper.getBaseContext()如果需要從另一個(gè)上下文中訪問上下文,則使用ContextWrapper。從ContextWrapper內(nèi)部引用的上下文通過getBaseContext()訪問。


查看完整回答
反對(duì) 回復(fù) 2019-06-18
?
守著一只汪

TA貢獻(xiàn)1872條經(jīng)驗(yàn) 獲得超4個(gè)贊

大多數(shù)答案已經(jīng)涵蓋了getContext()getApplicationContext()getBaseContext()很少被解釋。

方法getBaseContext()只有當(dāng)您有ContextWrapper..Android提供了一個(gè)ContextWrapper類,該類是圍繞現(xiàn)有的Context使用:

ContextWrapper wrapper = new ContextWrapper(context);

使用ContextWrapper它允許您“在不更改原始上下文的情況下修改行為”。例如,如果您有一個(gè)名為myActivity然后可以創(chuàng)建一個(gè)View有著不同的主題myActivity:

ContextWrapper customTheme = new ContextWrapper(myActivity) {
  @Override
  public Resources.Theme getTheme() { 
    return someTheme;
  }}View myView = new MyView(customTheme);

ContextWrapper是非常強(qiáng)大的,因?yàn)樗试S您覆蓋Context包括訪問資源的代碼(例如:openFileInput()getString()),與其他組件(例如:sendBroadcast()registerReceiver()),請(qǐng)求權(quán)限(例如,checkCallingOrSelfPermission())和解析文件系統(tǒng)位置(例如,getFilesDir()). ContextWrapper對(duì)于解決特定于設(shè)備/版本的問題或?qū)π枰舷挛牡囊晥D等組件應(yīng)用一次性自定義非常有用。

方法getBaseContext()可用于訪問ContextWrapper圍起來。例如,如果需要檢查“基本”上下文是否是ServiceActivityApplication:

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");
    }
    ...
  }}

或者,如果您需要調(diào)用方法的“未包裝”版本:

class MyCustomWrapper extends ContextWrapper {
  @Override
  public Drawable getWallpaper() {
    if (BuildInfo.DEBUG) {
      return mDebugBackground;
    } else {
      return getBaseContext().getWallpaper();
    }
  }}


查看完整回答
反對(duì) 回復(fù) 2019-06-18
?
慕容森

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊

getApplicationContext()-返回在應(yīng)用程序中運(yùn)行的所有活動(dòng)的上下文。

getBaseContext()-如果要從應(yīng)用程序中的另一個(gè)上下文訪問上下文,則可以訪問。

getContext()-只返回當(dāng)前正在運(yùn)行的活動(dòng)的上下文視圖。


查看完整回答
反對(duì) 回復(fù) 2019-06-18
  • 3 回答
  • 0 關(guān)注
  • 2908 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)