外部化 URL 的最佳方法是什么?

发布于 2024-10-02 16:22:58 字数 284 浏览 0 评论 0原文

我的应用程序中有 10 个 URL,目前是硬编码的。我想将其外部化并将其放入文件中,以便我的应用程序可以读取它。

执行此操作的最佳方法是什么?

private String appURL = "http://..."
private String storeURL = "http://..."
private String someURL = "http://..."

注意:我永远不会在我的应用程序中写入此文件。我希望开发人员能够打开该文件,并在必要时更改 URL。

I have 10 URL's in my application, that are currently hard-coded. I want to externalize this and put it in a file so that my application can read it in.

What is the best way of doing this?

private String appURL = "http://..."
private String storeURL = "http://..."
private String someURL = "http://..."

Note: I will never be writing out to this file in my app. I want the developer to be able to open up the file, and change the URL if necessary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奶气 2024-10-09 16:22:58

在 Android 应用程序中外部化字符串的最简单方法是使用字符串资源。在 res/values 文件夹中创建文件 uris.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="appUrl">http://...</string>
    <string name="storeUrl">http://...</string>
    <!-- etc /-->
</resources>

然后您可以通过以下方式访问字符串:

String appUrl = getString(R.string.appUrl);
String storeUrl = getString(R.string.storeUrl);
//..etc

getString()的方法>Context 类。另请记住,您应该等到 Context 初始化后再开始访问其资源。因此,不要早于 ActivityApplicationonCreate() 方法调用 getString()

Easiest way to externalize strings in an android application is to use string resources. Create file uris.xml at res/values folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="appUrl">http://...</string>
    <string name="storeUrl">http://...</string>
    <!-- etc /-->
</resources>

You then can then access the strings by:

String appUrl = getString(R.string.appUrl);
String storeUrl = getString(R.string.storeUrl);
//..etc

getString() is a method of Context class. Also keep in mind that you should wait until a Context will be initialized before starting access its resources. Hence invoke getString() not earlier then at onCreate() method of Activity or Application.

最舍不得你 2024-10-09 16:22:58

您可以将值放入 XML“配置”文件中,然后只需从 XML 文件中读取值。

读取 XML 文件的示例。

或者仅使用 Android 的本机 res 文件夹 XML 资源文件,如 康斯坦丁·布罗夫

You can put the values in a XML "configuration" file, then just read the values from the XML file.

Example of reading an XML file.

Or just use Android's native res folder XML resources files as pointed out by Konstantin Burov.

甜扑 2024-10-09 16:22:58

如果您希望能够编辑外部平面文件,您可以简单地在 SD 上创建一个文本文件(并具有一些默认值作为静态字符串或属性)。 onCreate 中所述的更改

然后,您的代码读取该文件(标准 Java IO)并拍摄 Activity 你也可以在 /assets 文件夹中有这样的文件;例如,参见这个

If you want ability to have external flat file to be editable you can simply create a text file on say SD (and have some defaults as static strings or properties). Then your code reads the file (standard Java IO) and pics the changes say in onCreate of your Activity

You can also have such file in /assets folder; see this for example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文