`
xiaoyaoniu
  • 浏览: 187543 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

Android中Context简介

 
阅读更多

Context字面意思是上下文,位于framework packageandroid.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄。很多方法需要通过 Context才能识别调用者的实例。

Context提供了关于应用环境全局信息的接口。它是一个抽象类,它的执行被Android系统所提供。它允许获取以应用为特征的资源和类型。同时启动应用级的操作,如启动Activitybroadcasting和接收intents

下面介绍Context的一些get方法,通过这些get方法可以获取应用环境全局信息:

1.public abstract Context getApplicationContext ()

Return the context of the single, global Application object of the current process.

2.public abstract ApplicationInfo getApplicationInfo ()

Return the full application info for this context's package.

3.public abstract ContentResolver getContentResolver ()

Return a ContentResolver instance for your application's package.

4.public abstract PackageManager getPackageManager ()

Return PackageManager instance to find global package information.

5.public abstract String getPackageName ()

Return the name of this application's package.

6.public abstract Resources getResources ()

Return a Resources instance for your application's package.

7.public abstract SharedPreferences getSharedPreferences (String name, int mode)

Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.

8.public final String getString (int resId)

Return a localized string from the application's package's default string table.

9.public abstract Object getSystemService (String name)

Return the handle to a system-level service by name. The class of the returned object varies by the requested name. Currently available names are:

 

两种类型的Context

androidcontext可以作很多操作,但是最主要的功能是加载和访问资源。在android中有两种context,一种是 application context,一种是activity context,通常我们在各种类和方法间传递的是activity context。比如一个activityonCreate

protected void onCreate(Bundle state) {
super.onCreate(state);
 
TextView label = new TextView(this);

//传递contextview control

label.setText("Leaks are bad");
 
setContentView(label);
}

activity context传递给view,意味着view拥有一个指向activity的引用,进而引用activity占有的资源:view hierachy, resource等。

 

 

内存泄露

这样如果context发生内存泄露的话,就会泄露很多内存。这里泄露的意思是gc没有办法回收activity的内存。

 

注释:为什么GC没有办法回收相应的内存,个人感觉是因为传递Context会增加对象指针的引用计数,所以基于智能指针技术的GC无法释放相应的内存。

 

当屏幕旋转的时候,系统会销毁当前的activity,保存状态信息,再创建一个新的。比如我们写了一个应用程序,它需要加载一个很大的图片,我们不希望每次旋转屏幕的时候都销毁这个图片,重新加载。实现这个要求的简单想法就是定义一个静态的Drawable,这样Activity 类创建销毁它始终保存在内存中。实现类似:

 

public class myactivity extends Activity {
private static Drawable sBackground;
protected void onCreate(Bundle state) {
super.onCreate(state);
 
TextView label = new TextView(this);
label.setText("Leaks are bad");
 
if (sBackground == null) {
sBackground = getDrawable(R.drawable.large_bitmap);
}
label.setBackgroundDrawable(sBackground);//drawable attached to a view

setContentView(label);
}
}

 

这段程序看起来很简单,但是却问题很大。当屏幕旋转的时候会有leak(即gc没法销毁activity)。我们刚才说过,屏幕旋转的时候系统会销毁当前的activity。但是当drawableview关联后,drawable保存了view reference,即sBackground保存了label的引用,而label保存了activity的引用。既然drawable不能销毁,它所引用和间接引用的都不能销毁,这样系统就没有办法销毁当前的activity,于是造成了内存泄露。gc对这种类型的内存泄露是无能为力的。避免这种内存泄露的方法是避免activity中的任何对象的生命周期长过activity,避免由于对象对 activity的引用导致activity不能正常被销毁。

 

 

为了防止内存泄露,我们应该注意以下几点:

  1. 不要让生命周期长的对象引用activity context,即保证引用activity的对象要与activity本身生命周期是一样的
  2. 对于生命周期长的对象,可以使用application context
  3. 避免非静态的内部类,尽量使用静态类,避免生命周期问题,注意内部类对外部对象引用导致的生命周期变化

 

application context

我们可以使用application contextapplication context伴随application的一生,与activity的生命周期无关。application context可以通过Context.getApplicationContext或者Activity.getApplication方法获取。

而制造Application context的方法在这里可以找到

http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables/708317#708317

 

Java里面通常是用一个static的变量(例如singleton之类的)来同步activity之间(程序里面类之间)的状态。在android里面比较靠谱的做法是用application context来关联这些状态。

每个activity都是context,里面包含了运行时的状态。同样application也有一个contextandroid会保证这个context是唯一的实例。

做一个你自己的application context需要继承android.app.Application,然后在appmanifest里面说明这个类。android会自动帮你创建你这个类的实例,接着你用Context.getApplicationContext()方法就能在各个activity

面获得这个application context了。

 class MyApp extends Application {

  private String myState;

  public String getState(){
    return myState;
  }
  public void setState(String s){
    myState = s;
  }
}

class Blah extends Activity {

  @Override
  public void onCreate(Bundle b){
    ...
    MyApp appState = ((MyApp)getApplicationContext());
    String state = appState.getState();
    ...
  }

 

 

 

分享到:
评论

相关推荐

    谈谈Android里的Context的使用

    大家好,今天给大家分享一下Android里的Context的一些用法. 这里大致可以分为两种:一是传递Context参数,二是调用全局的Context. 其实我们应用启动的时候会启动Application这个类,这个类是在AndroidManifest.xml...

    Android Context使用例子.

    Android Context使用例子.

    android全局context工具类

    android:name="工具类的路径" 调用: Toast.makeText(MainApplication.getContext(), "文本", Toast.LENGTH_SHORT).show(); 不管你想在项目的任何地方使用Context,只需要调用一下MainApplication.getContext()...

    Android Context 浅析

    简要介绍android context 的用法

    Android的context使用

    家好,今天给大家分享一下Android里的Context的一些用法,以前经常有人在群里问我比如我在一个工具类里的某个方法,或者View里需要调用Context.但是工具类还有View里没有这个上下文怎么办?为了解决大家的疑问,我...

    Android全局变量和Context

    Android全局变量和Context的实现方法

    android_context详解

    android_context详解,深入剖析

    012_android 之消息提示toast 和Context

    012_android 之消息提示toast 和Context视频教材,讲解的比较详细,有兴趣的可以学习下哦。

    避免 Android中Context引起的内存泄露

    本文主要介绍Android中Context引起的内存泄露的问题,这里对Context的知识做了详细讲解,说明如何避免内存泄漏的问题,有兴趣的小伙伴可以参考下

    Android 中Context的使用方法详解

    主要介绍了Android 中Context的使用方法详解的相关资料,希望通过本文大家能够理解掌握context的使用方法,需要的朋友可以参考下

    android中Context深入详解

    以下分别通过Context认知角度,继承关系,对象创建等方面android中Context做了深入的解释,一起学习下。 1、Context认知。 Context译为场景,一个应用程序可以认为是一个工作环境,在这个工作环境中可以存在许多场景...

    Android编程中context及全局变量实例详解

    本文实例讲述了Android编程中context及全局变量的用法。分享给大家供大家参考,具体如下: 今天在研究context的时候,对application和activity context有了一定的了解,下面是从网上复制过来的资料 Application ...

    android context理解

    android context理解

    android Context Menu With Icon

    android Context Menu With Icon

    Android-Context-Menu.Android.zip

    Android-Context-Menu.Android.zip,你可以很容易地添加令人敬畏的动画上下文菜单到你的应用程序。,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有...

    android demo,BaseAdapter传递context的应用

    android demo,BaseAdapter传递context的应用,

    Context-Menu.Android源码

    Context-Menu.Android源码,是一个很不错的C++代码,有兴趣的伙伴们抽时间可以看一下把。

    Android编程获取全局Context的方法

    主要介绍了Android编程获取全局Context的方法,实例分析了基于Application类获取全局Context的实现步骤与相关技巧,需要的朋友可以参考下

    详解Android中的Context抽象类

    主要介绍了Android中的Context抽象类,包括Context的实例化与获取App的Context等方法,需要的朋友可以参考下

Global site tag (gtag.js) - Google Analytics