在android中定义常量的最佳方法是静态类、接口还是xml资源?

Which is best way to define constants in android, either static class, interface or xml resource?(在android中定义常量的最佳方法是静态类、接口还是xml资源?)

本文介绍了在android中定义常量的最佳方法是静态类、接口还是xml资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 Web 服务从服务器获取数据的 android 应用程序,因为我有三组不同的 URL 来指向开发系统、测试服务器和实时服务器.每当我想提供测试/直播应用程序时,都很难更改 URL.所以我计划将其设置为可配置的,以便应用程序可以根据我的构建类型配置常量获取适当的 URL.所以,

I'm developing an android application which uses web service to get data from server, for that I'm having three different set of URLs to point development system, test server and live server. It's difficult to change URL whenever I want to give application for testing/live. so I planned to make it as configurable, so that application can get appropriate URL based on me build type configuration constant. So,

  • 这是保持这个常量的最好方法,java静态类或java公共接口或xml资源文件.?什么时候?为什么?
  • 哪个性能更好?什么时候?为什么?

例如:xml 资源

<integer name="config_build_type">0</integer>
<string-array name="url_authentication">
    <item >http://development.com/xxxx</item>
    <item >http://test.com/xxx</item>
    <item >http://example.com/xxx</item>
</string-array>

Java 静态常量

public class Config {
    public static final int BUILD_TYPE = 0; // 0 - development, 1 - test, 2 - live
    public static final String[] URL_AUTHENTICATION = {"http://development.com/", "http://test.com/", "http://example.com"};
}

推荐答案

两者之间有很大的区别,您可以在 XML 布局中引用项目资源.它们在应用程序上下文中可用,因此可以跨全局应用程序访问.使用项目资源的最大优势是易于访问,并且可以让您有效地组织您的项目.

There is a big difference between the two in that you can reference project resources in your XML layouts. They are available in the application context and are therefore accessible across the global application. The biggest advantages of using project resources is the ease of access and that they allow you to organize your project significantly.

static final 常量被编译成java字节码;项目资源在 apk 中被编译成二进制格式.访问其中任何一个都非常有效......如果两者之间存在差异,那最多是微不足道的.

static final constants are compiled into the java bytecode; project resources are compiled into a binary format within the apk. Accessing either is extremely efficient... if there is a difference between the two, it is trivial at most.

对于您应该如何在项目中使用资源/常量没有固定的规则.也就是说,我个人将资源用于可能需要在我的 XML java 代码中使用的值.另一方面,我通常将static final 常量用于 由我的java 代码使用并且特定于我的实现的值.

There isn't a set rule on how you should be using resources/constants in your project. That said, I personally use resources for values that I might need to use in my XML or java code. On the other hand, I typically use static final constants for values that will only be used by my java code and are specific to my implementation.

另请注意,根据设备的当前配置(即屏幕大小、区域设置等),可以在运行时加载 XML 资源.因此,在决定是在 XML 中还是直接在 .java 文件中声明常量时,您应该考虑到这一点.

Also note that it is possible to load XML resources at runtime depending on the device's current configuration (i.e. screen size, locale, etc.). So you should take this into consideration when deciding whether or not you should declare the constant in XML or directly in your .java files.

这篇关于在android中定义常量的最佳方法是静态类、接口还是xml资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在android中定义常量的最佳方法是静态类、接口还是xml资源?

基础教程推荐