I want my application to save/load settings (and preserve them when "upgrading"/installing a new version). Which component can I use for this? Registry wont be available I guess :-)
UPDATE: Settings like a string, a number or a boolean. Application settings, the user "manages" inside the app.
Android equivalent of Registry are SharedPreferences
You can easily get to the application private instance of SharedPreferences
through TAndroidHelper.PrivatePreferences
class function.
For retrieving stored settings you can then use following methods:
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;
In order to save settings you need to call edit
method on preference instance and then you can use following editor methods to store data:
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;
After you are done with modifying settings, you need to call apply
or commit
on editor. The only difference between the two is that apply just stores the data, and commit returns whether operation was successful or not.
Following is simple example that stores and reads boolean data.
Save data:
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;
Load data:
var
Pref: JSharedPreferences;
Value: Boolean;
begin
Pref := TAndroidHelper.PrivatePreferences;
Value := Pref.getBoolean(StringToJString('key'), False);
end;
You can also use any custom format (like Ini file) to save data in application local storage.