Android basic knowledge _ Context
I. Roles of Context 1.inheritance of API classes
2. Overview of the API Class A global information interface for the application environment. This is an abstract class and its implementation is provided by the Android system. It allows access to application-specific resources and classes, and can also call up application-level operations such as running Activity, broadcasting and receiving Intent.
II. The MainActivity. java code of the example project LearnContext used by Context is as follows:
Package com. example. learncontext; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. widget. imageView; import android. widget. textView; public class MainActivity extends Activity {// private TextView textView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState);/* example ① * // * TextView (Context context). The input parameter must be at least one Context, facilitating Resource access and global information * // textView = new TextView (this); // textView. setText ("Hello Android! ");/* SetText (int resid), input resource ID * Android must use Context to access global information ** // textView. setText (R. string. hello_world); // setContentView (textView); // System. out. println (getResources (). getText (R. string. hello_world);/* example ②: Get the icon resource */ImageView imageView = new ImageView (this); imageView. setImageResource (R. drawable. ic_launcher); setContentView (imageView) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}Uncomment the corresponding part and run the sample project to further experience the Context access to global information.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.