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 clearly.
转载于:https://stackoverflow.com/questions/3572463/what-is-context-on-android
Putting it simply:
As the name suggests, it's the context of current state of the application/object. It lets newly-created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity and package/application).
You can get the context by invoking getApplicationContext()
, getContext()
, getBaseContext()
or this
(when in a class that extends from Context
, such as the Application, Activity, Service and IntentService classes).
Typical uses of context:
Creating new objects: Creating new views, adapters, listeners:
TextView tv = new TextView(getContext());
ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);
Accessing standard common resources: Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:
context.getSystemService(LAYOUT_INFLATER_SERVICE)
getApplicationContext().getSharedPreferences(*name*, *mode*);
Accessing components implicitly: Regarding content providers, broadcasts, intent
getApplicationContext().getContentResolver().query(uri, ...);
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. Context is like a handle to the environment your application is currently running in. The activity object inherits the Context object.
For more information, look in Introduction to Android development with Android Studio - Tutorial.
An Android Context is an Interface (in the general sense, not in the Java sense; in Java, Context
is actually an abstract class!) that allows access to application specific resources and class and information about application environment.
If your android app was a web app, your context would be something similar to ServletContext
(I am not making an exact comparison here).
Your activities and services also extend Context
, so they inherit all those methods to access the environment information in which the app is running.
Context is a reference to the current object as this. Also context allows access to information about the application environment.
Context is basically for resource access and getting the environment details of the application(for application context) or activity (for activity context) or any other...
In order to avoid memory leak you should use application context for every components that needs a context object.... for more click here
What's Context
exactly?
Per the Android reference documentation, it's an entity that represents various environment data. It provides access to local files, databases, class loaders associated to the environment, services (including system-level services), and more. Throughout this book, and in your day-to-day coding with Android, you'll see the Context passed around frequently.
From the "Android in Practice" book, p. 60.
Several Android APIs require a Context
as parameter
If you look through the various Android APIs, you’ll notice that many of them take an android.content.Context
object as a parameter. You’ll also see that an Activity or a Service is usually used as a Context
. This works because both of these classes extend from Context
.
Think of it as the VM that has siloed the process the app or service is running in. The siloed environment has access to a bunch of underlying system information and certain permitted resources. You need that context to get at those services.
The class android.content.Context
provides the connection to the Android system and the resources of the project. It is the interface to global information about the application environment.
The Context also provides access to Android Services, e.g. the Location Service.
Activities and Services extend the Context
class.
Context
is an "interface" to the global information about an application environment. In practice, Context
is actually 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 the following picture, you can see a hierarchy of classes, where Context
is the root class of this hierarchy. In particular, it's worth emphasizing that Activity
is a descendant of Context
.
Just putting it out there for newbies;
So First understand Word Context :
In english-lib. it means:
"The circumstances that form the setting for an event, statement, or idea, and in terms of which it can be fully understood and assessed."
"The parts of something written or spoken that immediately precede and follow a word or passage and clarify its meaning."
Now take the same understanding to programming world:
context of current state of the application/object. It lets newly created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity, package/application)
You can get the context by invoking getApplicationContext()
, getContext(), getBaseContext()
or this
(when in the activity class).
To Get Context Anywhere in application use following code:
Create new class AppContext
inside your android application
public class AppContext extends Application {
private static Context context;
public void onCreate(){
super.onCreate();
AppContext.context = getApplicationContext();
}
public static Context getAppContext() {
return AppContext.context;
}
}
Now any time you want application context in non-activity class, call this method and you have application context.
Hope this help ;)
Consider Person-X is the CEO of a start-up software company.
There is a lead architect present in the company, this lead architect does all the work in the company which involves such as database, UI etc.
Now the CEO Hires a new Developer.
It is the Architect who tells the responsibility of the newly hired person based on the skills of the new person that whether he will work on Database or UI etc.
It's like access of android activity to the app's resource.
It's similar to when you visit a hotel, you want breakfast, lunch & dinner in the suitable timings, right?
There are many other things you like during the time of stay. How do you get these things?
You ask the room-service person to bring these things for you.
Here the room-service person is the context considering you are the single activity and the hotel to be your app, finally the breakfast, lunch & dinner have to be the resources.
Things that involve context are:
Another way to describe this: Consider context as remote of a TV & channel's in the television are resources, services, using intents etc - - - Here remote acts as an access to get access to all the different resources into foreground.
So, Remote has access to channels such as resources, services, using intents etc ....
Likewise ... Whoever has access to remote naturally has access to all the things such as resources, services, using intents etc
Different methods by which you can get context
getApplicationContext()
getContext()
getBaseContext()
this
(when in the activity class)Example:
TextView TV=new TextView(this);
this
-> refers to the context of the current activity.
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.
Different invoking methods by which you can get context 1. getApplicationContext(), 2. getContext(), 3. getBaseContext() 4. or this (when in the activity class).
Context is Instances of the the class android.content.Context provide the connection to the Android system which executes the application. For example, you can check the size of the current device display via the Context.
It also gives access to the resources of the project. It is the interface to global information about the application environment.
The Context class also provides access to Android services, e.g., the alarm manager to trigger time based events.
Activities and services extend the Context class. Therefore they can be directly used to access the Context.
Simple Example to understand context
in android :
Every boss has an assistant to look after, to do all less important and time consuming tasks. If a file or a cup of coffee is needed, assistant is on the run. Some bosses barely know what’s going on in the office, so they ask their assistants regarding this too. They do some work themselves but for most other things they need help of their assistants.
In this scenario,
Boss – is the Android application
Assistant – is context
Files/Cup of coffee – are resources
We generally call context when we need to get information about different parts of our application like Activities, Applications etc.
Some operations(things where assistant is needed) where context is involved:
Loading common resources Creating dynamic views Displaying Toast messages Launching Activities etc. Different ways of getting context:
getContext()
getBaseContext()
getApplicationContext()
this
What can you do with it ?
Ways to get context :