【问题标题】:Where to store global constants in an Android application?在 Android 应用程序中将全局常量存储在哪里?
【发布时间】:2013-02-10 14:40:40
【问题描述】:

我会知道存储全局常量的最佳做法是什么,这些常量会在编译时随环境(调试、预生产、生产、发布等)而改变。

在 iOS 中,我曾经将所有全局常量保存在头文件中,并使用预处理器宏对其进行更改,请参阅此答案:

Where to store global constants in an iOS application?

我应该为 Android 使用什么解决方案?

【问题讨论】:

    标签: java android global-variables constants


    【解决方案1】:

    这里有非常简单的解决方案

    public class Constants {
        /**
         * Object key prams when pass the json object from server.
         */
        public static final String KEY_EMAIL = "email";
        public static final String KEY_PASSWORD = "password";
        public static final String KEY_DEVICE_TOKEN = "device_token";
        public static final String KEY_DEVICE_TYPE = "device_type";
        public static final String KEY_NAME = "name";
        public static final String KEY_COUNTRY_CODE = "country_code";
        public static final String KEY_PHONE_CODE = "phone-code";
        public static final String KEY_GENDER = "gender";
        public static final String KEY_DATE_OF_BIRTH = "date_of_birth";
        public static final String KEY_USER_ID = "user_id";
        public static final String KEY_LIMIT = "limit";
        public static final String KEY_DRIVER_ID = "driver_id";
        public static final String KEY_LONGTITUDE = "logitude";
        public static final String KEY_LATTITUDE = "lattitude";
        public static final String KEY_RATING = "rating";
        public static final String KEY_DETAILS = "details";
        public static final String KEY_ACCESS_TOKEN= "access_token";
        /**
         * Fragments name
         */
        public static final String FRAG_ETA = "ETA";
        public static final String FRAG_ACCOUNT_FRAGMENT = "ACCOUNT_FRAGMENT";
        public static final String FRAG_SETTING_FRAGMENT = "SETTING_FRAGMENT";
        public static final String FRAG_MAP_FRAGMENT = "MAP_FRAGMENT";
        public static final String FRAG_FEEDBACK = "FEEDBACK";
        public static final String FRAG_RATE_FRAGMENT = "RATE_FRAGMENT";
    
        public static final String USA_CODE = "+1";
    
        public static final String DISTANCE_SEARCH = "DISTANCE_SEARCH";
    
    
    }
    

    快乐编码

    【讨论】:

    • 在某些情况下我得到一些空值。 (用户收到新消息,但在某些情况下,按上述方法存储的内容解析器的 Uri 为空)
    【解决方案2】:

    属性文件

    我们在<project>/<package>/src/main/assets/config.properties下存储了一个属性文件

    加载属性

       private static final String PROPS_NAME = "config.properties";
       private static Properties configuration;
    
       ...
       public static void init(Context ctx) {
    
            configuration = new Properties();
            InputStream rawResource = resources.getAssets().open(PROPS_NAME);
            configuration.load(rawResource);
    

    【讨论】:

    • 还要查看 SharedPreferences 和 Resources
    【解决方案3】:

    另一种解决方案可能是使用资源文件(如果您满足于仅存储字符串值)。

    这可用于存储常量,例如此应用程序管理的帐户:

    例如。 WelcomeActivity.java

    AccountManager am = AccountManager.get(WelcomeActivity.this);
    Account account = am.getAccountsByType(getResources().getString(R.string.ACCOUNT_TYPE))[0];
    

    例如。 res/values/strings.xml

    <resources>
        <string name="ACCOUNT_NAME">com.acme.MyAccountSignature</string>
    </resources>
    

    这还允许您在无需重新编译的情况下对其进行修改(类似于您通常解耦翻译的方式,最适合使用 strings.xml 文件)。

    【讨论】:

    • 如果没有Context 的实例,我可以得到这些常量吗?我想在当前不需要Context 工作的库中使用一些常量。
    • 好问题,是的,您应该可以,请参阅@Gangnus 在this question 中给出的答案
    【解决方案4】:

    在您的基础包文件夹中创建一个类常量

    (或创建一个接口而不是一个类,因此不需要每次都引用该类,但是由于代码可读性,这是bad practice,但它会起作用)

    public static final 值填充它。

    此外,classinterface 也可以声明为abstract

    【讨论】:

    • 好的,但是如何在“编译时”更改它?例如,我不想将所有值存储三次(针对 3 个不同的环境),并且只在需要时才对所有值进行评论。
    • 你的意思是项目有 3 种不同的环境?然后创建一个库应用程序,在其中创建常量类,然后可以将其作为库添加到任意数量的项目中。
    • 不,我的意思是 3 个环境,例如一个项目的 debug、preprod 或 prod。例如,我想更改我的 web 服务 API 'prepod.xxx.com/ws'、'xxx.com/ws'、'192.168.1.42/ws' 的基本 URL(就像我在问题中引用的答案),只有 1 个常量需要更改比如“DEBUG”、“PREPROD”、“PROD”
    【解决方案5】:

    使用public static final values.并将它们保存在单独的java文件中,如下所示:

        static String QC    = "http:/************";
        static String DEV   = "http:/************";
        static String CLOUD = "http:/************";
    
    
        static String SERVICEURL = CLOUD ; //Use this SERVICEURL in your code at run time
    

    【讨论】:

    • 这行得通...但我永远不会在源文件中放置 URL。为了卫生,这种数据必须在属性文件、数据库配置表、命令行参数或系统环境变量中。
    【解决方案6】:

    如果常量的值取决于环境(密度、语言环境等),那么您应该使用资源来存储它们(整数、字符串、维度等)。

    在另一种情况下,您可以将全局常量放在一个文件中(最佳实践 - 为每组常量使用前缀)或将局部常量放在相关的类中(例如,Intent 包含标志、附加组件、类别等)。

    【讨论】:

    • 据我了解,您应该使用 Maven(自动构建工具)。您可以为不同的配置确定不同的常量,Maven 将为您构建不同的 apk(每个 apk 使用一组常量)。有关详细信息,请参阅此问题stackoverflow.com/questions/13867148/…
    猜你喜欢
    • 2012-03-13
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 2021-10-18
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多