What is 'Context' on Android?

前端 未结 30 3661
南旧
南旧 2020-11-21 04:46

In Android programming, what exactly is a Context class and what is it used for?

I read about it on the developer site, but I am unable to understand it

30条回答
  •  庸人自扰
    2020-11-21 05:31

    Context is an interface to global information about an application environment. It's an abstract class whose implementation is provided by the Android system.

    Context allows access to application-specific resources and classes, as well as calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

    Here is Example

     public class MyActivity extends Activity {
    
          public void Testing() {
    
          Context actContext = this; /*returns the Activity Context since   Activity extends Context.*/
    
          Context appContext = getApplicationContext();    /*returns the context of the single, global Application object of the current process. */
    
          Button BtnShowAct1 = (Button) findViewById(R.id.btnGoToAct1);
          Context BtnContext = BtnShowAct1.getContext();   /*returns the context of the View. */
    

    For more details you can visit http://developer.android.com/reference/android/content/Context.html

提交回复
热议问题