【Android】關於上下文的種種傳說

Contextandroid

  1. 中文直譯爲「上下文」
  2. SDK中對其說明以下

  Interface to global information about an application environment. This is an abstract class whose implementationapp

  is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls ide

  for application-level operations such as launching activities, broadcasting and receiving intents, etc測試

 

 

 

看一段測試代碼this

private Context mContext;
    public void method() { 
        mContext = this;
        mContext = MainActivity.this; 
        mContext = getApplicationContext(); 
        mContext = getBaseContext(); 
        Log.v("huangc","####  mContext="+ this );
        Log.v("huangc","####  MainActivity.this="+ MainActivity.this );
        Log.v("huangc","####  getApplicationContext()="+getApplicationContext() );
        Log.v("huangc","####  getBaseContext()="+ getBaseContext());
     }

看日誌輸出spa

####  mContext=com.example.test4intent.MainActivity@302ffdcb
####  MainActivity.this=com.example.test4intent.MainActivity@302ffdcb
####  getApplicationContext()=android.app.Application@39c519a8
####  getBaseContext()=android.app.ContextImpl@26ad4cc1

可見大多數狀況下.net

【1】 this == MainActivity.this日誌

【2】this != getApplicationContext() != getBaseContext()code

 

 

 

參考文檔orm

http://blog.csdn.net/qinjuning/article/details/7310620

相關文章
相關標籤/搜索