如何在 Android 应用程序中存储设置?

How to store settings in Android Application?

我希望我的应用程序 save/load 设置(并在“升级”/安装新版本时保留它们)。 我可以为此使用哪个组件?我猜注册表不可用 :-)

更新:字符串、数字或布尔值等设置。应用设置,用户在应用内“管理”。

您可以使用 SharedPreferences 来存储您的设置

示例:在 Java 因为您没有定义您编写 Android 应用程序的语言

public static SharedPreferences getPreferences(Context context) {
    String appName = context.getResources().getString(R.string.app_name);
    return context.getSharedPreferences(appName, MODE);
}

public static void clearSharedPreferenceFile(Context context) {
    getEditor(context).clear().apply();
}

public static SharedPreferences.Editor getEditor(Context context) {
    return getPreferences(context).edit();
}

public static void writeString(Context context, String key, String value) {
    getEditor(context).putString(key, value).apply();
}

public static String readString(Context context, String key) {
    return getPreferences(context).getString(key, "");
}

public static void writeInt(Context context, String key, int value) {
    getEditor(context).putInt(key, value).apply();
}

public static int readInt(Context context, String key) {
    return getPreferences(context).getInt(key, Integer.MIN_VALUE);
}
public static void writeBoolean(Context context, String key, boolean value) {
    getEditor(context).putBoolean(key, value).apply();
}

public static boolean readBoolean(Context context, String key) {
    return getPreferences(context).getBoolean(key, false);
}

您可以使用 SharedPreferences、文件或数据库来保存您的数据

Android 相当于注册表 SharedPreferences

您可以通过TAndroidHelper.PrivatePreferences class函数轻松获取SharedPreferences的应用程序私有实例。

要检索存储的设置,您可以使用以下方法:

  JSharedPreferences = interface(IJavaInstance)
    ['{E44179D1-B961-4316-A8B0-45B52A482FA7}']
    function &contains(key: JString): Boolean; cdecl;
    function edit: JSharedPreferences_Editor; cdecl;
    function getAll: JMap; cdecl;
    function getBoolean(key: JString; defValue: Boolean): Boolean; cdecl;
    function getFloat(key: JString; defValue: Single): Single; cdecl;
    function getInt(key: JString; defValue: Integer): Integer; cdecl;
    function getLong(key: JString; defValue: Int64): Int64; cdecl;
    function getString(key: JString; defValue: JString): JString; cdecl;
    function getStringSet(key: JString; defValues: JSet): JSet; cdecl;
    procedure registerOnSharedPreferenceChangeListener(listener: JSharedPreferences_OnSharedPreferenceChangeListener); cdecl;
    procedure unregisterOnSharedPreferenceChangeListener(listener: JSharedPreferences_OnSharedPreferenceChangeListener); cdecl;
  end;

为了保存设置,您需要在首选项实例上调用 edit 方法,然后您可以使用以下编辑器方法来存储数据:

  JSharedPreferences_Editor = interface(IJavaInstance)
    ['{A162AACF-DD6D-466E-838B-363E6B092CA4}']
    procedure apply; cdecl;
    function clear: JSharedPreferences_Editor; cdecl;
    function commit: Boolean; cdecl;
    function putBoolean(key: JString; value: Boolean): JSharedPreferences_Editor; cdecl;
    function putFloat(key: JString; value: Single): JSharedPreferences_Editor; cdecl;
    function putInt(key: JString; value: Integer): JSharedPreferences_Editor; cdecl;
    function putLong(key: JString; value: Int64): JSharedPreferences_Editor; cdecl;
    function putString(key: JString; value: JString): JSharedPreferences_Editor; cdecl;
    function putStringSet(key: JString; values: JSet): JSharedPreferences_Editor; cdecl;
    function remove(key: JString): JSharedPreferences_Editor; cdecl;
  end;

修改完设置后,需要在编辑器上调用applycommit。两者唯一的区别是apply只是存储数据,commitreturns不管操作成功与否

以下是存储和读取布尔数据的简单示例。

保存数据:

uses
  AndroidApi.Helpers,
  Androidapi.JNI.GraphicsContentViewText,


var
  Pref: JSharedPreferences;
  PrefEditor: JSharedPreferences_Editor;
  Success: Boolean;
begin
  Pref := TAndroidHelper.PrivatePreferences;

  PrefEditor := Pref.edit;
  PrefEditor.putBoolean(StringToJString('key'), True);
  PrefEditor.apply;
  // or
  Success := PrefEditor.commit;
end;

加载数据:

var
  Pref: JSharedPreferences;
  Value: Boolean;
begin
  Pref := TAndroidHelper.PrivatePreferences;     
  Value := Pref.getBoolean(StringToJString('key'), False);
end;

您还可以使用任何自定义格式(如 Ini 文件)将数据保存在应用程序本地存储中。