unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.ScrollBox,
FMX.Memo, FMX.StdCtrls, FMX.Controls.Presentation, FMX.Edit, FMX.Layouts;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Memo1: TMemo;
Layout1: TLayout;
Label2: TLabel;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
FMX.Helpers.Android, //需要引入
Androidapi.JNI.JavaTypes,//需要引入
Androidapi.JNI.GraphicsContentViewText,//需要引入
FMX.Platform.Android,//需要引入
Androidapi.JNIBridge,//需要引入
Androidapi.JNI.Provider,//需要引入
Androidapi.Helpers;//需要引入
{$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID}
//定义一个查询方法
procedure QueryContact(AName: string; AList: TStrings);
var
cursorContactsPhone: JCursor;
selection: string;
oprojection: TJavaObjectArray;
oselectionArgs: TJavaObjectArray;
FieldIndex: Integer;
begin
if AList <> nil then
AList.Clear;
oprojection := nil;
oselectionArgs := nil;
if AName.Length > 0 then
begin
oprojection := TJavaObjectArray.Create(1);
oselectionArgs := TJavaObjectArray.Create(1);
oprojection.Items[0] := TJContacts_PeopleColumns.JavaClass.DISPLAY_NAME;
selection := JStringToString(TJContacts_PeopleColumns.JavaClass.DISPLAY_NAME)+' LIKE “%' + AName + '%”';
end;
//select projection from 联系人数据库 where selection
cursorContactsPhone := SharedActivity.getContentResolver.query
(TJCommonDataKinds_Phone.JavaClass.CONTENT_URI,
oprojection, {要查询的字段名,nil的全部}
StringToJString(selection),{Where条件}
oselectionArgs, { 这里是Where语句的条件参数们,我上面图方便,写死在Where条件中了,没使用参数 }
StringToJString(''));
if AList <> nil then
while (cursorContactsPhone.moveToNext) do
begin
//获取字段的ColumnIndex
FieldIndex := cursorContactsPhone.getColumnIndex
(TJContacts_PeopleColumns.JavaClass.DISPLAY_NAME);
//读字段内容
AList.Add(JStringToString(cursorContactsPhone.getString(FieldIndex)));
end;
cursorContactsPhone.close;
end;
//调用查询方法
procedure TForm1.Button1Click(Sender: TObject);
var
s: TStrings;
begin
s := TStringList.Create;
QueryContact(Edit1.Text, s); //查询姓王的人
Memo1.Text := s.Text;
s.Free;
end;
end.