你正在用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
我試過但沒有改變任何東西。 –