I have just started on the Android development path using Delphi XE5, and am trying to build a simple application that needs to be able to persist some entered information (configuration).
我刚刚开始使用Delphi XE5开发Android开发路径,正在尝试构建一个简单的应用程序,该应用程序需要能够持久化一些输入的信息(配置)。
I have figured out, that the Android class SharedPreferences probably is the easiest way to do this, but I can't figure out how to access this class from Delphi XE5 FMX Mobile.
我已经知道,Android类SharedPreferences可能是最简单的方法,但是我不知道如何从Delphi XE5 FMX Mobile访问这个类。
I have tried searching for "SharedPreferences" in the help, but it returns nothing. A search for "Shared Preferences", on the other hand, gives me too much.
我尝试在帮助中搜索“共享首选项”,但它没有返回任何内容。另一方面,搜索“共享的偏好”给了我太多的东西。
1 个解决方案
#1
17
In a nutshell, add the required API units to the uses clause - the key ones in your case are AndroidApi.Jni.JavaTypes
, AndroidApi.Jni.App
, and AndroidApi.Jni.GraphicsContentViewText
, together with FMX.Helpers.Android
for some glue code - and call it pretty much like you might in Java. Java classes are exposed as interface types with an initial J; in practice the Android API uses nested classes quite a lot, and since Delphi doesn't support nested interface types, these become ParentClassName_ChildClassName:
简而言之,将所需的API单元添加到use子句中——在您的案例中,关键的是AndroidApi.Jni。将javatype AndroidApi.Jni。应用程序和AndroidApi.Jni。GraphicsContentViewText FMX.Helpers一起。Android的一些粘合代码——和你在Java中的调用非常相似。Java类被公开为具有初始J的接口类型;实际上,Android API经常使用嵌套类,而且由于Delphi不支持嵌套接口类型,这些类就变成了parentclassname_childclassclassclassname:
var
Prefs: JSharedPreferences;
Editor: JSharedPreferences_Editor;
I: Integer;
F: Single;
S: string;
begin
Prefs := SharedActivity.getPreferences(TJActivity.JavaClass.MODE_PRIVATE);
Editor := Prefs.edit;
Editor.putInt(StringToJString('MyIntKey'), 999);
Editor.putFloat(StringToJString('MyFloatKey'), 123.456);
Editor.putString(StringToJString('MyStrKey'), StringToJString('This is a test'));
Editor.apply;
I := Prefs.getInt(StringToJString('MyIntKey'), 0);
F := Prefs.getFloat(StringToJString('MyFloatKey'), 0);
S := Prefs.getString(StringToJString('MyIntKey'), StringToJString(''));
That said, I've recently put out a simple TCustomIniFile
descendant that wraps the SharedPreferences
API - see here for info:
也就是说,我最近发布了一个简单的TCustomIniFile后代,它封装了SharedPreferences API——参见这里的信息:
http://delphihaven.wordpress.com/2013/09/12/a-few-xe5-related-bits/
http://delphihaven.wordpress.com/2013/09/12/a-few-xe5-related-bits/
In mapping the API to TCustomIniFile
, one small issue I found is the fact SharedPreferences
keys are strongly typed, and there doesn't seem to be a way to find out in advance what type a given key has (keys in TCustomIniFile
, in contrast, are weakly typed). Because of this, for reading, I use the getAll
method to retreive all keys and values as a Map
/JMap
(Java dictionary object in other words) and read individual keys from there.
在将API映射到TCustomIniFile时,我发现了一个小问题:SharedPreferences键是强类型的,而且似乎没有一种方法可以预先知道给定键的类型(相比之下,TCustomIniFile中的键是弱类型的)。因此,对于读取,我使用getAll方法将所有键和值作为Map/JMap (Java dictionary对象)进行retreive,并从那里读取单个键。
#1
17
In a nutshell, add the required API units to the uses clause - the key ones in your case are AndroidApi.Jni.JavaTypes
, AndroidApi.Jni.App
, and AndroidApi.Jni.GraphicsContentViewText
, together with FMX.Helpers.Android
for some glue code - and call it pretty much like you might in Java. Java classes are exposed as interface types with an initial J; in practice the Android API uses nested classes quite a lot, and since Delphi doesn't support nested interface types, these become ParentClassName_ChildClassName:
简而言之,将所需的API单元添加到use子句中——在您的案例中,关键的是AndroidApi.Jni。将javatype AndroidApi.Jni。应用程序和AndroidApi.Jni。GraphicsContentViewText FMX.Helpers一起。Android的一些粘合代码——和你在Java中的调用非常相似。Java类被公开为具有初始J的接口类型;实际上,Android API经常使用嵌套类,而且由于Delphi不支持嵌套接口类型,这些类就变成了parentclassname_childclassclassclassname:
var
Prefs: JSharedPreferences;
Editor: JSharedPreferences_Editor;
I: Integer;
F: Single;
S: string;
begin
Prefs := SharedActivity.getPreferences(TJActivity.JavaClass.MODE_PRIVATE);
Editor := Prefs.edit;
Editor.putInt(StringToJString('MyIntKey'), 999);
Editor.putFloat(StringToJString('MyFloatKey'), 123.456);
Editor.putString(StringToJString('MyStrKey'), StringToJString('This is a test'));
Editor.apply;
I := Prefs.getInt(StringToJString('MyIntKey'), 0);
F := Prefs.getFloat(StringToJString('MyFloatKey'), 0);
S := Prefs.getString(StringToJString('MyIntKey'), StringToJString(''));
That said, I've recently put out a simple TCustomIniFile
descendant that wraps the SharedPreferences
API - see here for info:
也就是说,我最近发布了一个简单的TCustomIniFile后代,它封装了SharedPreferences API——参见这里的信息:
http://delphihaven.wordpress.com/2013/09/12/a-few-xe5-related-bits/
http://delphihaven.wordpress.com/2013/09/12/a-few-xe5-related-bits/
In mapping the API to TCustomIniFile
, one small issue I found is the fact SharedPreferences
keys are strongly typed, and there doesn't seem to be a way to find out in advance what type a given key has (keys in TCustomIniFile
, in contrast, are weakly typed). Because of this, for reading, I use the getAll
method to retreive all keys and values as a Map
/JMap
(Java dictionary object in other words) and read individual keys from there.
在将API映射到TCustomIniFile时,我发现了一个小问题:SharedPreferences键是强类型的,而且似乎没有一种方法可以预先知道给定键的类型(相比之下,TCustomIniFile中的键是弱类型的)。因此,对于读取,我使用getAll方法将所有键和值作为Map/JMap (Java dictionary对象)进行retreive,并从那里读取单个键。