开发者

Trying to explain context in Android to a friend

I am trying to explain context to a friend. Context as written in the documentation is that it is interface to the system resources (sensors, vibrator etc) but would this mean memory, CPU etc?

And also, is context a reference to an activity? I mean is it possible to compare that a context is equal to a uiviewcontroller in iOS programming and application contex开发者_运维知识库t is the app delegate? I am still learning Android so I might not be the best to make an answer. If any one can provide a thorough answer on this I would be very thankful.

Regards not a native English speaker so I would also like to know what context means in programming.


I am trying to explain context to a friend. Context as written in the documentation is that it is interface to the system resources (sensors, vibrator etc) but would this mean memory, CPU etc?

No, "interface" to memory and CPU are implicitly provided by the execution and memory model of Java language.

And also, is context a reference to an activity?

Activities are a kind of context. You can get a full list of classes that inherit from Context in the docs under "Indirect Subclasses".

I mean is it possible to compare that a context is equal to a uiviewcontroller in iOS programming and application context is the app delegate? I am still learning Android so I might not be the best to make an answer. If any one can provide a thorough answer on this I would be very thankful.

I think the two are quite different. You might check out Tasks and the Back Stack to see more about Activities as loosely-coupled, separate units. A subclass of Application in your app may be similar to an app delegate in iOS.

Regards not a native English speaker so I would also like to know what context means in programming.

The English definition might help:

2 : the situation in which something happens : the group of conditions that exist where and when something happens

In programming "context" is quite close to this definition. The context often tells a function or object the answers to things like:

  • Where am I?
  • Where is resource X?
  • Is feature Y available?
  • What was I just doing? (especially common in the case of C)

Honestly, I think you can get very far in Android programming without understanding Context.


Context in android is a basic interface to access several things:

  1. System services. Vibrator, sensors and so, as you already mentioned.
  2. Resources(strings.xml, shared preferences and so on).
  3. Views, belonging to the context.

Context can be described as a programming environment for your code. It is a context in which your code executes.

Context can not be linked just to an activity, Application, Dialog, Service and others also implements it.

Context in this particular situation can be represented as a cloud of objects and things you can access, which are visible to you. From activity you can access views, services and resources. In application context there are no view, but still you see resources and services. And so on.


As the name suggests, its 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, package/application)

You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in the activity class).

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, ...);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜