本文小编为大家详细介绍“ContentProvider启动流程源码分析”,内容详细,步骤清晰,细节处理妥当,希望这篇“ContentProvider启动流程源码分析”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
ContentProvider大家都知道,
ContentProviderApplication的onCreateContentProvider那
ContentProviderApp启动
App启动时,AMS会通过跨进程
BinderApplicationThreadbindApplication      public final void bindApplication(String processName, ApplicationInfo appInfo,
                List<ProviderInfo> providers, ComponentName instrumentationName,
                ProfilerInfo profilerInfo, Bundle instrumentationArgs,
                IInstrumentationWatcher instrumentationWatcher,
                IUiAutomationConnection instrumentationUiConnection, int debugMode,
                boolean enableBinderTracking, boolean trackAllocation,
                boolean isRestrictedBackupMode, boolean persistent, Configuration config,
                CompatibilityInfo compatInfo, Map services, Bundle coreSettings,
                String buildSerial, boolean autofillCompatibilityEnabled) {
            // 拼接AppBindData,发送给ActivityThread的H
            sendMessage(H.BIND_APPLICATION, data);
        }这个方法主要作用是,拼接
AppBindDataActivityThreadHandler mHHandlerMessagehandleBindApplication(data)private void handleBindApplication(AppBindData data) {
    final InstrumentationInfo ii;
    // 创建 mInstrumentation 实例
    if (ii != null) {
        //创建ContextImpl
        final ContextImpl appContext = ContextImpl.createAppContext(this, pi);
        try {
            //创建mInstrumentation实例
            final ClassLoader cl = appContext.getClassLoader();
            mInstrumentation = (Instrumentation) cl.loadClass(data.instrumentationName.getClassName()).newInstance();
        } catch (Exception e) {}
    } else {
        mInstrumentation = new Instrumentation();
    }
    Application app;
    try {
        // 创建 Application 实例
        app = data.info.makeApplication(data.restrictedBackupMode, null);
        mInitialApplication = app;
        // 如果不是backup模式,则调用installContentProvider,启动ContentProvider
         if (!data.restrictedBackupMode) {
                if (!ArrayUtils.isEmpty(data.providers)) {
                    //启动ContentProvider
                    installContentProviders(app, data.providers);
                    mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
                }
            }
        try {
            //调用Application的onCreate
            mInstrumentation.callApplicationOnCreate(app);
        } catch (Exception e) { }
    }
}这个方法非常长,主要做的事情有以下四点:
创建一个
ContentImpl创建一个
Instrument创建
Application如果不是
backupinstallContentProvidersContentProvider调用
ApplicationonCreateinstallContentProviders
private void installContentProviders(Context context, List<ProviderInfo> providers) {
        final ArrayList<ContentProviderHolder> results = new ArrayList<>();
        // 遍历所有的providers
        for (ProviderInfo cpi : providers) {
            // 开始启动ContentProvider
            ContentProviderHolder cph = installProvider(context, null, cpi,
                    false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
             results.add(cph);
        }
        // 将成功启动的provider存储到AMS的mProviderMap中
        ActivityManager.getService().publishContentProviders(getApplicationThread(), results);
    }这个方法,循环遍历所有待启动的
ContentProviderinstallProvider private ContentProviderHolder installProvider(Context context,
            ContentProviderHolder holder, ProviderInfo info,
            boolean noisy, boolean noReleaseNeeded, boolean stable) {
                // 反射创建ContentProvider
                final java.lang.ClassLoader cl = c.getClassLoader();
                LoadedApk packageInfo = peekPackageInfo(ai.packageName, true);
                localProvider = cl.loadClass(className).newInstance();
                provider = localProvider.getIContentProvider();
                // 调用ContentProvider的attachInfo方法
                localProvider.attachInfo(c, info);
    }这个方法,通过反射创建
ContentProviderattachInfo private void attachInfo(Context context, ProviderInfo info, boolean testing) {
        // 调用onCreate方法
       ContentProvider.this.onCreate();
}在
ContentProviderattachInfoonCreateContentProvider以上就是ContentProvider启动流程源码分析的详细内容,更多关于ContentProvider启动流程源码分析的资料请关注九品源码其它相关文章!