Context interface for accessing global information
App Resources: strings, drawable resources, etc.
Let's look at a particle that uses context to scope resources
Public classMainactivity extends Activity {String TAG="Carloz"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); //Activity itself is a ContextTextView TV =NewTextView (mainactivity. This); Tv.settext ("Hello Carloz"); Tv.settext (R.string. Hello_world); Setcontentview (TV); String Str= (String) getapplicationcontext (). Getresources (). GetText (R.string. Hello_world); LOG.D (TAG, str); //output 08-16 11:59:27.402:d/carloz (13255): Hello world!ImageView IV=NewImageView (mainactivity. This); Iv.setimageresource (R.drawable.ic_launcher); Setcontentview (iv); }}
From the above code can be seen to create a new TextView or ImageView at least one parameter context,activity itself is a Context, so you can copy.
Tv.settext (R.string.hello_world); A string ID is passed in, and the code inside the SetText API is GetContext (). Getresources (). GetText (Resid); As you can see, getting resources requires a context.
Android Context Role