你正在用java编程。按照太阳的惯例,我认为你有义务使用config.propeties文件。
我会给你一个快速和完整的教程,让你进入这个问题。我真的推荐你使用这种方法,因为大多数程序员都这样做。
我会给你一个快速教程如何使这个文件。在哪里放。以及如何从中获取数据。
开始。
将一个文件config.properties到资产的文件夹:config.properties的
样品
[email protected]
errorTextColor=\#FF0000
serverPort=1234
方法如何访问和检索config.properties
公共静态字符串数据getConfigurationPropertiesValue(字符串值, 上下文上下文)尝试{ 资源资源= cont ext.getResources(); AssetManager assetManager = resources.getAssets();
try {
InputStream inputStream = assetManager.open("config.properties");
Properties properties = new Properties();
properties.load(inputStream);
return properties.getProperty(value);
} catch (IOException e) {
Log.e("getConfigurationPropertiesValue",
"Failed to open config property file");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
文学: http://en.wikipedia.org/wiki/.properties http://www.mkyong.com/java/java-properties-file-examples/
编辑:
您还可以使用sharedPreferences有像将数据添加/删除数据/更新数据,对数据更多的控制权。 SharedPreferences更像是一个android的SQLite数据库,它使用了一个很好的api,所以你并不需要知道数据库或SQL的位置。
为了使用它,你需要创建你的数据。你只需要做一次。或更多,如果用户决定他有一个冲动来清除设置 - >应用程序中的应用程序数据。
创建数据:
public static void create(Context cw) {
SharedPreferences sharedPreferences = cw.getSharedPreferences(
ANDROID_MESSENGER, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("isRegistered", false);
editor.putString("phoneNumber", null);
editor.putString("callingCode", null);
String uuid = UUID.randomUUID().toString();
editor.putString("token", uuid);
editor.putBoolean("internetOnly", false);
editor.putBoolean("logToDev", true);
editor.putBoolean("dataTransfer", true);
Log.i("create", "Generating Token: " + uuid);
editor.commit();
}
Accesing现有数据:
public static String getToken(Context cw) {
SharedPreferences sharedPreferences = cw.getSharedPreferences(
ANDROID_MESSENGER, Activity.MODE_PRIVATE);
return sharedPreferences.getString("token", null);
}
更新数据:
public static void setPhoneNumber(Context cw, String phoneNumber) {
SharedPreferences sharedPreferences = cw.getSharedPreferences(
ANDROID_MESSENGER, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("phoneNumber", phoneNumber);
editor.commit();
}
因为,如果存在的数据或将检查的机构,如果用户删除它你可以使用一些简单的东西,比如一个附加变量,如果所有y都应该是真的我们的数据已配置。或者应该有一个来自appshared首选项的检查方法。
干杯!
你应该将Preferences类调用为'Preferences prefs = new Preferences(this);' – Dulanga
我试过但没有改变任何东西。 –