这是Stackoverflow上一位网友的回答:
原地址:http://stackoverflow.com/questions/3572463/what-is-context-in-android
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()
orthis
(when in the activity class).Typical uses of context:
- Creating New objects: Creating new views, adapters, listeners:
1 TextView tv = new TextView(getContext()); 2 ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);- Accessing Standard Common Resources: Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:
1 context.getSystemService(LAYOUT_INFLATER_SERVICE) 2 getApplicationContext().getSharedPreferences(*name*, *mode*);- Accessing Components Implicitly: Regarding content providers, broadcasts, intent
getApplicationContext().getContentResolver().query(uri, ...);
时间: 2024-08-10 21:29:06