在Android中存储配置 [英] Storing configs in android

查看:296
本文介绍了在Android中存储配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出放置配置类型数据的最佳位置.目前,所有内容都使用Java进行了硬编码 例如,API端点URL,API密钥等. 他们可以只使用字符串资源吗?还是在静态类中?

I'm trying to work out where the best place to put config type data is. At the moment everything is hardcoded in java e.g API endpoint URLs, API keys etc. Can they just go in strings resources? or in a static class?

旁注-保持这样的API密钥安全吗?

side note - is it safe to keep API keys like this?

推荐答案

有一些可用的选项:

1.静态类(类似于Config.java)或单例对象.例如:

1.Static class (something like Config.java) or singleton object. For example:

public enum Config {
    INSTANCE;

    public static final String CLOUD_ID = "8888888888";
}

2.String资源(strings.xml或单独的字符串文件,如api.xml).例如:

2.String resources (strings.xml or separate string file like api.xml). For example:

 <string name="google_cloud_message_register_id" translatable="false">8888888888</string>

3.Integer资源(xml文件,例如integers.xml).例如:

3.Integer resources (xml file like integers.xml). For example:

<resources>
    <integer name="category_id">777</integer>
</resources>

4. SharedPreferences 存储.例如:

mSharedPreferences.edit().putString(AUTH_EMAIL_KEY, authEmail).commit(); // to save
mSharedPreferences.getString(AUTH_EMAIL_KEY, null); // to get

5.外部存储(即文件)

6. SQLite 数据库

7.Gradle构建脚本配置.例如:

7.Gradle build script configuration. For example:

// in build.gradle
buildTypes {
    release {
        buildConfigField "String", "API", "\"https://api.com/api/\""
    }
}

BuildConfig.API // access in Java code

对于API端点URL,公共API密钥等.我更喜欢使用Gradle构建脚本配置.它非常灵活,可以在各种构建类型中使用不同的值.

For API endpoint URLs, public API keys etc. I prefer to use Gradle build script configuration. It's very flexible and different values can be used with various build types.

加密的SQLite表可以用于存储微妙的私有数据,但是保存私有数据的最佳方法是从受保护的外部服务器加载它们.

Encrypted SQLite table can be used to store delicate private data but the best way to save private data is load them from protected external server, I think.

这篇关于在Android中存储配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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