Understanding and use of Android basic knowledge _context

Source: Internet
Author: User

I. The role of the Contextinheritance relationships for 1.API classes
Overview of the 2.API classA global information interface about 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, or it can invoke application-level operations such as running activity, broadcasting, and receiving intent intents.

Ii. Examples of context usethe Mainactivity.java code for the example project Learncontext 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 incoming parameter is at least one context, easy access to resources, global information *///textview = new TextView (this);//// Textview.settext ("Hello android!"); * SetText (int resid), incoming resource ID * Android access to global information must use context * *///textview.settext (r.string.hello_world);//setcontentview (TextView);//system.out.println (Getresources (). GetText (R.string.hello_world));/* Example ②: Get 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 PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return trUE;}} 
Dismiss the corresponding section comment and run the sample project to further experience context access to global information.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Understanding and use of Android basic knowledge _context

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.