首页 > 编程知识 正文

get()方法(getResources()详解)

时间:2023-05-05 05:01:24 阅读:124053 作者:1866

/* * * returncodegetactivity (.get resources )/code.*/finalpublicresourcesgetresources ) (if ) mhost==null ) ) } finalfragmentcontrollermfragments=fragment controller.create controller (newhostcallbacks (); classhostcallbacksextendsfragmenthostcallbackfragmentactivity { publichostcallbacks (} { super ) fragment activity.this/* FragmentActivity时初始化并FragmentHostCallback ) ),将4个参数的构筑方法分别代入mActivity,将mActivity强力转用到mContext中执行的mHost的gggivity 获取资源是调用活动的获取资源

publicabstractclasscontext { publicabstractresourcesgetresources (; }在以前的文章中,我们提到了Context的具体实现类是ContextImpl,但是让我们来看看ContextImpl的getResources是如何实现的。

@ overridepublicresourcesgetresources ({ return m resources; }直接返回到mResources,看看mResources是从哪里分配的

隐私保护客户端时间(contextimplcontainer,ActivityThread mainThread,LoadedApk packageInfo,IBinder activityToken,user handll 配置覆盖配置,int createDisplayWithId ) { mOuterContext=this; mMainThread=mainThread; mActivityToken=activityToken; mFlags=flags; if (user==null (user=process.my user handle ) ); } mUser=user; mPackageInfo=packageInfo; mresourcesmanager=resources manager.getinstance (;//获取资源资源资源=package info.get资源(mainthread ); //mResources赋值mResources=resources; m display=(createdisplaywithid==display.invalid _ display )? display : mresourcesmanager.getadjusteddisplay (display id,m resources.getdisplayadjustments ) ); }正如上一篇文章所述,创建ContextImpl是在创建ActivityThread时通过调用getSystemContext方法创建的

publiccontextimplgetsystemcontext () synchronized ) this ) if ) msystemcontext==null ) msystemcontext=context impl }返回}创建staticcontextimplcreatesystemcontext (activitythreadmainthread )加载的apk加载的apk软件包信息

= new LoadedApk(mainThread); 创建ContextImpl ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY); context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(), context.mResourcesManager.getDisplayMetrics()); return context;}

我们在回到getResources方法 ,上面看到 packageInfo.getResources(mainThread); packageInfo就是LoadedApk这里在看下LoadedApk的getResources是如何实现的

public Resources getResources(ActivityThread mainThread) { if (mResources == null) { mResources = mainThread.getTopLevelResources(mResDir, mSplitResDirs, mOverlayDirs, mApplicationInfo.sharedLibraryFiles, Display.DEFAULT_DISPLAY, this); } return mResources;}

mResources 是通过 ActivityThread的getTopLevelResources方法获取的

Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs, String[] libDirs, int displayId, LoadedApk pkgInfo) { return mResourcesManager.getResources(null, resDir, splitResDirs, overlayDirs, libDirs, displayId, null, pkgInfo.getCompatibilityInfo(), pkgInfo.getClassLoader());}

这里是 mResourcesManager 的getResources方法

public @NonNull Resources getResources(@Nullable IBinder activityToken, @Nullable String resDir, @Nullable String[] splitResDirs, @Nullable String[] overlayDirs, @Nullable String[] libDirs, int displayId, @Nullable Configuration overrideConfig, @NonNull CompatibilityInfo compatInfo, @Nullable ClassLoader classLoader) { try { Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "ResourcesManager#getResources"); final ResourcesKey key = new ResourcesKey( resDir, splitResDirs, overlayDirs, libDirs, displayId, overrideConfig != null ? new Configuration(overrideConfig) : null, // Copy compatInfo); classLoader = classLoader != null ? classLoader : ClassLoader.getSystemClassLoader(); return getOrCreateResources(activityToken, key, classLoader); } finally { Trace.traceEnd(Trace.TRACE_TAG_RESOURCES); }} private @NonNull Resources getOrCreateResources(@Nullable IBinder activityToken, @NonNull ResourcesKey key, @NonNull ClassLoader classLoader) { synchronized (this) { if (DEBUG) { Throwable here = new Throwable(); here.fillInStackTrace(); Slog.w(TAG, "!! Get resources for activity=" + activityToken + " key=" + key, here); } if (activityToken != null) { final ActivityResources activityResources = getOrCreateActivityResourcesStructLocked(activityToken); // Clean up any dead references so they don't pile up. ArrayUtils.unstableRemoveIf(activityResources.activityResources, sEmptyReferencePredicate); // Rebase the key's override config on top of the Activity's base override. if (key.hasOverrideConfiguration() && !activityResources.overrideConfig.equals(Configuration.EMPTY)) { final Configuration temp = new Configuration(activityResources.overrideConfig); temp.updateFrom(key.mOverrideConfiguration); key.mOverrideConfiguration.setTo(temp); } ResourcesImpl resourcesImpl = findResourcesImplForKeyLocked(key); if (resourcesImpl != null) { if (DEBUG) { Slog.d(TAG, "- using existing impl=" + resourcesImpl); } return getOrCreateResourcesForActivityLocked(activityToken, classLoader, resourcesImpl); } // We will create the ResourcesImpl object outside of holding this lock. } else { // Clean up any dead references so they don't pile up. ArrayUtils.unstableRemoveIf(mResourceReferences, sEmptyReferencePredicate); // Not tied to an Activity, find a shared Resources that has the right ResourcesImpl ResourcesImpl resourcesImpl = findResourcesImplForKeyLocked(key); if (resourcesImpl != null) { if (DEBUG) { Slog.d(TAG, "- using existing impl=" + resourcesImpl); } return getOrCreateResourcesLocked(classLoader, resourcesImpl); } // We will create the ResourcesImpl object outside of holding this lock. } } // If we're here, we didn't find a suitable ResourcesImpl to use, so create one now. ResourcesImpl resourcesImpl = createResourcesImpl(key); synchronized (this) { ResourcesImpl existingResourcesImpl = findResourcesImplForKeyLocked(key); if (existingResourcesImpl != null) { if (DEBUG) { Slog.d(TAG, "- got beat! existing impl=" + existingResourcesImpl + " new impl=" + resourcesImpl); } resourcesImpl.getAssets().close(); resourcesImpl = existingResourcesImpl; } else { // Add this ResourcesImpl to the cache. mResourceImpls.put(key, new WeakReference<>(resourcesImpl)); } final Resources resources; if (activityToken != null) { resources = getOrCreateResourcesForActivityLocked(activityToken, classLoader, resourcesImpl); } else { resources = getOrCreateResourcesLocked(classLoader, resourcesImpl); } return resources; }}

 

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。