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

查看:511
本文介绍了哪个是在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资源文件. 何时?为什么?
  • 哪个具有更好的性能?何时?为什么?
  • which is the best way to keep this constants, java static class or java public interface or xml resource file.? When? Why?
  • which gives better performance?, When? Why?

例如: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资源)的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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