Android basic knowledge _ Context

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.