在没有上下文的情况下访问资源 [英] Accessing Resources without a Context

查看:33
本文介绍了在没有上下文的情况下访问资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将配置(例如 URLs/etc)放入资源文件夹中以供实用程序类使用.但是,我不想从任何地方的活动中传递上下文.我希望能够通过路径名访问资源(似乎 assets/就是为此用途而设计的),而无需使用上下文来访问资源.

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() 时都必须传入一个 Context 将完全是一种浪费.

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.

对于本地资源,请使用该解决方案.

For local resources use that solution.

这篇关于在没有上下文的情况下访问资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆