德尔福访问Android的共享preferences类 [英] Accessing Android's SharedPreferences class from Delphi

查看:222
本文介绍了德尔福访问Android的共享preferences类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Delphi XE5 Android的发展道路,并正尝试构建需要能够坚持一些输入的信息(配置)的简单应用程序。

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).

我已经想通了,这是Android类共享preferences 大概是做最简单的方法,但我无法弄清楚如何从德尔福XE5 FMX手机访问这个类。

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.

我试图寻找的帮助共享preferences,但它没有返回值。搜索共享preferences,而另一方面,给了我太多。

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.

推荐答案

在简单地说,添加所需的API单位的使用条款 - 关键的人,你的情况是 AndroidApi.Jni.JavaTypes AndroidApi.Jni.App AndroidApi.Jni.GraphicsContentViewText ,加上 FMX.Helpers.Android 对于一些胶水code - 并调用它更pretty的就像你可能在Java中。 Java类被公开为具有初始Ĵ接口类型;在实践中,Android的API使用嵌套类相当多,而且因为德尔福不支持嵌套的接口类型,这些成为ParentClassName_ChildClassName:

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(''));

这是说,我最近推出了一个简单的 TCustomIniFile 的后裔,它包装的共享preferences API - 在这里看到的信息:

That said, I've recently put out a simple TCustomIniFile descendant that wraps the SharedPreferences API - see here for info:

的http:// delphihaven .word press.com / 2013/09/12 / A-几-xe5相关比特/

在API映射到 TCustomIniFile ,一个小小的问题,我发现的是共享preferences 键是强类型的,似乎没有成为一个办法,找出提前什么类型给定的密钥具有(在 TCustomIniFile 键,相反,是弱类型) 。正因为如此,对于阅读,我用的是 GETALL 方法中检索所有键和值作为地图 / JMAP (即Java的字典对象),并从那里读取各个按键。

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.

这篇关于德尔福访问Android的共享preferences类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆