访问资源没有上下文上下文、资源

2023-09-12 10:04:52 作者:梦破碎

我试图把配置,如URL /等,成为一个资源文件夹中的实用工具类使用。但是,我并不想通过从活动中随处可见。我希望能够通过一个路径名访问资源(好像资产/设计用于这种用途),不使用上下文访问的资源。

I'm trying to put configuration, such as URLs/etc, into a resource folder for a utility class to use. However, I don't want to pass the Context from the activities everywhere. I would like to be able to access a resource via a path name (seems like assets/ was designed for this use), without using a context to access the resource.

在这种特殊情况下,我想一个单独使用的东西在配置时,它的实例化。它有没有必要,除了实例化过程中,有一次,从资源的任何东西。因此,必须传入语境每一次的getInstance()被称为将是一个完整的浪费。

In this particular case, I want a singleton to use something in configuration when it's instantiated. It has no need for anything from resources besides that one time during instantiation. Therefore, having to pass in a Context every time getInstance() is called would be a complete waste.

此外,这是特定于应用程序的配置,并且不应该被存储在一个共享的系统文件或类似的东西。

Also, this is specific to the App's configuration, and should not be in stored in a shared system file or anything like that.

推荐答案

使用

Resources.getSystem().getString(android.R.string.someuniversalstuff)

您可以在您的应用程序中使用这是绝对无处不在,即使在静态常量宣言!但对于系统资源仅

You can use it ABSOLUTELY EVERYWHERE in your application, even in static constants declaration! But for system resources only.

对于本地资源使用该解决方案。