Android中Context到底是什么以及用法

  这是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() or this (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

Android中Context到底是什么以及用法的相关文章

Android中Context的总结及其用法

在语句 android中我们经常遇到这样的情况,在创建一个对象的时候往往需要传递一个this参数,比如:语句 MyView mView = new MyView(this),要求传递一个this参数,这个this究竟指的是什么东西呢? 其实这里的this指的就是当前的Activity.this,是这个语句所在的Activity的this.Activity.this取的是这个Activity的Context,那么这个Context究竟是什么东西呢?它起到什么作用呢? Context 按照英文字面意

Android中Context详解 ---- 你所不知道的Context

转载至 :http://blog.csdn.net/qinjuning 前言:本文是我读<Android内核剖析>第7章 后形成的读书笔记 ,在此向欲了解Android框架的书籍推荐此书. 大家好,  今天给大家介绍下我们在应用开发中最熟悉而陌生的朋友-----Context类 ,说它熟悉,是应为我们在开发中 时刻的在与它打交道,例如:Service.BroadcastReceiver.Activity等都会利用到Context的相关方法 : 说它陌生,完全是 因为我们真正的不懂Context

Android中Context详解 ---- 你所不知道的Context (转载)

Android中Context详解 ---- 你所不知道的Context (转载) http://blog.csdn.net/qinjuning 大家好,  今天给大家介绍下我们在应用开发中最熟悉而陌生的朋友-----Context类 ,说它熟悉,是应为我们在开发中 时刻的在与它打交道,例如:Service.BroadcastReceiver.Activity等都会利用到Context的相关方法 : 说它陌生,完全是 因为我们真正的不懂Context的原理.类结构关系.一个简单的问题是,一个应用

Android中Context详解 ---- 你所不知道的Context(转)

Android中Context详解 ---- 你所不知道的Context(转)                                              本文出处 :http://blog.csdn.net/qinjuning 前言:本文是我读<Android内核剖析>第7章 后形成的读书笔记 ,在此向欲了解Android框架的书籍推荐此书. 大家好,  今天给大家介绍下我们在应用开发中最熟悉而陌生的朋友-----Context类 ,说它熟悉,是应为我们在开发中 时刻的在与它打

Android中Context的理解与使用技巧

Context类,时刻的在与它打交道,例如:Service.BroadcastReceiver.Activity等都会利用到Context的相关方法. 但是不懂Context的原理.类结构关系.一个简单的问题是,一个应用程序App中存在多少个Context实例对象呢? Context,中文直译为"上下文",SDK中对其说明如下: Interface to global information about an application environment. This is an ab

Android中Context解析

Context概念 当我们访问当前应用的资源,启动一个新的activity的时候都需要提供Context. Context是一个抽象基类,我们通过它访问当前包的资源(getResources.getAssets)和启动其他组件(Activity.Service.Broadcast)以及得到各种服务(getSystemService),当然,通过Context能得到的不仅仅只有上述这些内容.对Context的理解可以来说:Context提供了一个应用的运行环境,在Context的大环境里,应用才可

Android中Context具体解释 ---- 你所不知道的Context

                                                                                                                                    本文原创 ,转载必须注明出处 :http://blog.csdn.net/qinjuning 前言:本文是我读<Android内核剖析>第7章 后形成的读书笔记 ,在此向欲了解Android框架的书籍推荐此书. 大家好,  今天给大家

【三】6.Android 中 Context 的理解及使用

[一]Context的作用:访问全局信息 Context是访问全局信息的接口,比如说应用程序的资源(图片资源.字符串资源.其他资源...), 所以一些常用组件就会继承Context,目的就是为了访问资源,比如说Activity以及将要学习的Service. 创建项目来看下如何通过Context进行资源的访问. public class MainActivity extends Activity{     private TextView tv;          @Override     pr

Android中Context详解

Context,中文直译为“上下文”,SDK中对其说明如下: Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well