从Delphi访问Android的SharedPreferences类 [英] Accessing Android's SharedPreferences class from Delphi
问题描述
我已经弄清楚,Android类 SharedPreferences 可能是最简单的方式来做,但我无法弄清楚如何从Delphi XE5 FMX Mobile访问这个类。
我已经尝试在帮助中搜索SharedPreferences ,但它什么也没有返回。另一方面,搜索共享首选项给了我太多的东西。
简而言之, API单元到uses子句 - 您的案例中的关键是 AndroidApi.Jni.JavaTypes
, AndroidApi.Jni.App
和 AndroidApi.Jni.GraphicsContentViewText
,连同 FMX.Helpers.Android
为一些胶水代码 - 并调用它很像你可能在Java中。 Java类被暴露为具有初始J的接口类型;在实践中,Android API使用嵌套类很多,由于Delphi不支持嵌套接口类型,所以它们变为ParentClassName_ChildClassName:
var
Prefs:JSharedPreferences;
编辑器:JSharedPreferences_Editor;
I:整数;
F:单
S:string;
begin
Prefs:= SharedActivity.getPreferences(TJActivity.JavaClass.MODE_PRIVATE);
编辑:= 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(''));
说,我最近推出了一个简单的 TCustomIniFile
descendant包裹 SharedPreferences
API - 请参阅这里了解信息:
http://delphihaven.wordpress.com/2013/09/12/a -few-xe5-related-bits /
将API映射到 TCustomIniFile
,一个小我发现的问题是事实 SharedPreferences
键是强类型的,并且似乎没有一种方法可以提前找出给定键的类型(键在 TCustomIniFile
,相比之下,是弱键入)。因此,为了阅读,我使用 getAll
方法将所有键和值作为 Map
/ JMap
(Java字典对象,换句话说),并从那里读取单个密钥。
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).
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.
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.
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:
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:
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.
这篇关于从Delphi访问Android的SharedPreferences类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!