安卓下保存参数配置的实现

很多时候我们开发的软件需要向用户提供软件参数设置功能。
对于软件配置参数的保存,如果是window软件通常我们会采用ini文件进行保存。
如果是Android应用,我们最适合采用什么方式保存软件配置参数呢?
Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。

整体代码如下:


 


unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  AndroidApi.Jni.JavaTypes, AndroidApi.Jni.App, FMX.Helpers.Android,
  AndroidApi.Jni.GraphicsContentViewText,
  Androidapi.JNI,
  Androidapi.JNIBridge, FMX.Edit, FMX.Controls.Presentation,Androidapi.Helpers,
  FMX.StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    button2: TButton;
    Button1: TButton;
    procedure button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);

  private
    { Private declarations }
    function StrToJCharSequence(const ASource: string): JCharSequence;
    function JCharSequenceToString(const ASource: JCharSequence): string;
    function JStringToString(const JStr: JString): string;
    function StringToJString(const Str: string): JString;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}



{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
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('ip1'), StringToJString(Edit1.Text));
  Editor.putString(StringToJString('ip2'), StringToJString(Edit2.Text));
  Editor.apply;
end;

procedure TForm1.button2Click(Sender: TObject);
var
  Prefs: JSharedPreferences;
  Editor: JSharedPreferences_Editor;
  //I: Integer;
  //F: Single;
  S,t: string;
begin
  Prefs := SharedActivity.getPreferences(TJActivity.JavaClass.MODE_PRIVATE);
  //I := Prefs.getInt(StringToJString('MyIntKey'), 0);
  //F := Prefs.getFloat(StringToJString('MyFloatKey'), 0);
  S := JStringToString(Prefs.getString(StringToJString('ip1'), StringToJString('')));
  t:= JStringToString(Prefs.getString(StringToJString('ip2'), StringToJString('')));
  Edit1.Text:=s;
  Edit2.Text:=t;
end;

function TForm1.JCharSequenceToString(const ASource: JCharSequence): string;
begin
 Result := JStringToString(ASource.toString);
end;

function TForm1.JStringToString(const JStr: JString): string;
begin
  if JStr = nil then
    Result := ''
  else
    Result:= JNIStringToString(TJNIResolver.GetJNIEnv, JNIString((JStr as ILocalObject).GetObjectID));
end;

function TForm1.StringToJString(const Str: string): JString;
var
  LocalRef: JNIObject;
begin
  LocalRef := StringToJNIString(TJNIResolver.GetJNIEnv, Str);
  Result := TJString.Wrap(LocalRef);
  TJNIResolver.DeleteLocalRef(LocalRef);
end;

function TForm1.StrToJCharSequence(const ASource: string): JCharSequence;
begin
  Result := TJCharSequence.Wrap((StringToJString(ASource) as ILocalObject).GetObjectID);
end;

end.

分享到: