Context总结

Posted by Lazy on July 2, 2016

##context继承结构

  • context的直接子类有2个,一个是ContextWrapper,一个是ContextImpl,那么从名字上就可以看出,ContextWrapper是上下文功能的封装类,而ContextImpl则是上下文功能的实现类。而ContextWrapper又有三个直接的子类,ContextThemeWrapper、Service和Application。其中,ContextThemeWrapper是一个带主题的封装类,而它有一个直接子类就是Activity。

  • ==由此,其实我们就已经可以得出结论了,Context一共有三种类型,分别是Application、Activity和Service。这三个类虽然分别各种承担着不同的作用,但它们都属于Context的一种,而它们具体Context的功能则是由ContextImpl类去实现的。==
  • 那么context能做那些事情? 弹出Toast、启动Activity、启动Service、发送广播、操作数据库等等等等都需要用到Context。由于Context的具体能力是由ContextImpl类去实现的,因此在绝大多数场景下,Activity、Service和Application这三种类型的Context都是可以通用的。不过有几种场景比较特殊,比如启动Activity,还有弹出Dialog。出于安全原因的考虑,Android是不允许Activity或Dialog凭空出现的,一个Activity的启动必须要建立在另一个Activity的基础之上,也就是以此形成的返回栈。如何想要用不是activity的context打开activity的时候增加一个FLAG,因为打开一个activity需要一个栈我们指定一个栈就可以了。而Dialog则必须在一个Activity上面弹出(除非是System Alert类型的Dialog),因此在这种场景下,我们只能使用Activity类型的Context,否则将会出错。 ##Context数量
  • Context一共有Application、Activity和Service三种类型,因此一个应用程序中Context数量的计算公式就可以这样写: Context数量 = Activity数量 + Service数量 + 1
    上面的1代表着Application的数量,因为一个应用程序中可以有多个Activity和多个Service,但是只能有一个Application。 ##Application Context的设计
  • 我们获取一个Application需要在xm里面注册然后在activity里面调用getApplication()getApplicationContext()来获取实例!我们通过测试得知这两个获取都是一个实例,
  • 那么为什么会有这两个方法呢? getApplication()方法的语义性非常强,一看就知道是用来获取Application实例的,但是这个方法只有在Activity和Service中才能调用的到。那么也许在绝大多数情况下我们都是在Activity或者Service中使用Application的,但是如果在一些其它的场景,比如BroadcastReceiver中也想获得Application的实例,这时就可以借助getApplicationContext()方法了,也就是说,getApplicationContext()方法的作用域会更广一些,任何一个Context的实例,只要调用getApplicationContext()方法都可以拿到我们的Application对象。
- 我们发现还有一个getBaseContext()方法这个方法是获取什么的呢?
		getBaseContext()方法得到的是一个ContextImpl对象,也就是说像Application、Activity这样的类其实并不会去具体实现Context的功能,而仅仅是做了一层接口封装而已,Context的具体功能都是由ContextImpl类去完成的。
- 我们下源码




protected void attachBaseContext(Context base) {
    if (mBase != null) {
        throw new IllegalStateException("Base context already set");
    }
    mBase = base;
}

/**
 * @return the base context as set by the constructor or setBaseContext
 */
public Context getBaseContext() {
    return mBase;
}

@Override
public AssetManager getAssets() {
    return mBase.getAssets();
}

@Override
public Resources getResources() {
    return mBase.getResources();
}

@Override
public ContentResolver getContentResolver() {
    return mBase.getContentResolver();
}

@Override
public Looper getMainLooper() {
    return mBase.getMainLooper();
}

@Override
public Context getApplicationContext() {
    return mBase.getApplicationContext();
}

@Override
public String getPackageName() {
    return mBase.getPackageName();
}

@Override
public void startActivity(Intent intent) {
    mBase.startActivity(intent);
}

@Override
public void sendBroadcast(Intent intent) {
    mBase.sendBroadcast(intent);
}

@Override
public Intent registerReceiver(
    BroadcastReceiver receiver, IntentFilter filter) {
    return mBase.registerReceiver(receiver, filter);
}

@Override
public void unregisterReceiver(BroadcastReceiver receiver) {
    mBase.unregisterReceiver(receiver);
}

@Override
public ComponentName startService(Intent service) {
    return mBase.startService(service);
}

@Override
public boolean stopService(Intent name) {
    return mBase.stopService(name);
}

@Override
public boolean bindService(Intent service, ServiceConnection conn,
        int flags) {
    return mBase.bindService(service, conn, flags);
}

@Override
public void unbindService(ServiceConnection conn) {
    mBase.unbindService(conn);
}

@Override
public Object getSystemService(String name) {
    return mBase.getSystemService(name);
}

...... } ``` - 因为我们的activity和application都是直接或间接的继承ContextWrapper,我们只需要看这个就行了,所以我们得知attachBaseContext()方法其实是由系统来调用的,它会把ContextImpl对象作为参数传递到attachBaseContext()方法当中,从而赋值给mBase对象,之后ContextWrapper中的所有方法其实都是通过这种委托的机制交由ContextImpl去具体实现的,所以说ContextImpl是上下文功能的实现类是非常准确的。

##使用Application的问题

public class MyApplication extends Application {
	
	public MyApplication() {
		String packageName = getPackageName();
		Log.d("TAG", "package name is " + packageName);
	}
	
}

这个就会报空指针而下面的写法就没有问题

public class MyApplication extends Application {
	
	@Override
	public void onCreate() {
		super.onCreate();
		String packageName = getPackageName();
		Log.d("TAG", "package name is " + packageName);
	}
	
}
  • 这个是为什么呢? 我们前面不是说了,ContextWrapper中有一个attachBaseContext()方法,在这个里面才去初始化获取到mBase对象才能调用它里面的方法!

##TIP

  • 我们平时要尽量使用ApplicationContext不要使用activity的上下文,因为如果我们在activity里面进行一些耗时操作,就会倒置当前的activity没办法及时销毁因为你持有当前界面的context
  • 博客原文http://blog.csdn.net/guolin_blog/article/details/47028975