Context相关

Context相关

Context是抽象类,位于content包下,是应用程序运行的上下文,提供程序需要的基础信息。

image

Interface to global information about an application environment. This is an abstract class whose implementation is provided bythe Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities,broadcasting and receiving intents, etc.

Context有两个子类,ContextWrapper,ContextImpl,ContextWrapper是Context的包装类,同时也是ContextThemeWrapper、Apllication、Service的父类。

ContextWrapper

ContextWrapper位于content包下,重写了Context的方法,部分委托实现一些功能。方便提供给子类调用。

class ContextWrapper extends Context {

    public ContextWrapper(Context base) {
        mBase = base;
    }

    public void startActivity(Intent intent,    Bundle options) {
     mBase.startActivity(intent, options);
    }

    ......
}

ContextThemeWrapper

主要是设置主题相关,Activity继承当前类,可以设置主题信息。

public class ContextThemeWrapper extends ContextWrapper {
    public void applyOverrideConfiguration(Configuration overrideConfiguration) {
    if (mResources != null) {
        throw new IllegalStateException(
                "getResources() or getAssets() has already been called");
    }
    if (mOverrideConfiguration != null) {
        throw new IllegalStateException("Override configuration has already been set");
    }
    mOverrideConfiguration = new Configuration(overrideConfiguration);
}

private void initializeTheme() {
    final boolean first = mTheme == null;
    if (first) {
        mTheme = getResources().newTheme();
        final Resources.Theme theme = getBaseContext().getTheme();
        if (theme != null) {
            mTheme.setTo(theme);
        }
    }
    onApplyThemeResource(mTheme, mThemeResource, first);
}

}

Application

Application位于android.view包下,继承ContextWrapper,是全局应用生命周期,监听Activity生命周期。

class Application extends ContextWrapper implements ComponentCallbacks2 {
    interface ActivityLifecycleCallbacks {...
    
    interface OnProvideAssistDataListener {...

    
}

当应用程序第一次启动的时候,创建Application对象,在ActivityThread.java中,启动应用程序调用handleBindApplication()方法创建Application对象。

void handleBindApplication(AppBindData data) {
    
     
    
     try {
         final ApplicationInfo instrApp = new ApplicationInfo();
        ......

        ContextImpl appContext = createBaseContextForActivity(r);
    }

    ......

}

启动Activity是在performLaunchActivity()方法中,

Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    
    ......

      ContextImpl appContext = createBaseContextForActivity(r);
    
     try {
        Application app = r.packageInfo.makeApplication(false, mInstrumentation);
    }

    ......

}

Service

Service位于app包下,在handleCreateService()方法创建服务,

 private void handleCreateService(CreateServiceData data) {
    
    ......

    try {
      

        ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
        context.setOuterContext(service);

        Application app = packageInfo.makeApplication(false, mInstrumentation);
        service.attach(context, this, data.info.name, data.token, app,
                ActivityManager.getService());
        service.onCreate();
        ......
       
    } catch (Exception e) {
       
    }
}

ContextImpl

ContextImpl位于app包下,是Context的实现类,主要是实现Context具体功能,文件写入、发送广播、启动服务等一系列操作。

class ContextImpl extends Context {

......

 public Resources getResources() {
        return mResources;
    }

    @Override
    public PackageManager getPackageManager() {
        if (mPackageManager != null) {
            return mPackageManager;
        }

        IPackageManager pm = ActivityThread.getPackageManager();
        if (pm != null) {
            // Doesn't matter if we make more than one instance.
            return (mPackageManager = new ApplicationPackageManager(this, pm));
        }

        return null;
    }

    @Override
    public ContentResolver getContentResolver() {
        return mContentResolver;
    }

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

Context、Activity、Application区别及使用?

1、Context是抽象类,Activity、Application是Context子类。

2、单例模式下注意传入context生命周期是否是ApplicationContext,否则造成内存泄漏问题。

3、使用场景:

  • 传入Activity的Context对象一般弹窗、跳转页面、启动绑定服务、发送广播、加载资源都是可以
  • 传入Application和Service的Context,不能加载资源、弹窗、跳转;

你可能感兴趣的:(Context相关)