In Android, context is used for many operations, but mainly for loading and accessing resources. That is why all widgets receive the Context parameter in their constructor. In a typical Android application, you usually have two kinds of context, activity, and application. Usually this is the first one that the developer passes to classes and methods that need context:
You can get the context by calling getApplicationContext (), getContext (), getBaseContext () or this (when in an activity class).
Typical use of context:
Creating new objects: creating new views, adapters, listeners:
TextView tv = new TextView (getContext ()); Adapter ListAdapter = new SimpleCursorAdapter (getApplicationContext (), ...);
Access to standard shared resources: services such as LAYOUT_INFLATER_SERVICE, SharedPreferences:
context.getSystemService(LAYOUT_INFLATER_SERVICE)
getApplicationContext(). getSharedPreferences (, );
: ,
,
getApplicationContext(). getContentResolver(). query (uri,...);
, maintaning
, , , :
- - (
,
)
- ,
,
.
WeakReference ,
ViewRoot W,
- .