本文整理自网络,侵删。
结果:1.将权限打开Read contacts设置为True,不然报图一的错误。2.搜索空没有问题,但搜索名字时报错了,占时没跟踪,哪位大神了解的M我,多谢了。
实例代码: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;
implementationuses 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<JString>; oselectionArgs: TJavaObjectArray<JString>; FieldIndex: Integer;begin if AList <> nil then AList.Clear; oprojection := nil; oselectionArgs := nil; if AName.Length > 0 then begin oprojection := TJavaObjectArray<JString>.Create(1); oselectionArgs := TJavaObjectArray<JString>.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.
来源:https://www.cnblogs.com/FKdelphi/p/4816457.html
相关阅读 >>
Delphi 调用viewer-windows10 图像浏览器
Delphi firedac 如何按整型(byte)读取 mysql tinyint(1) 类型字段?
更多相关阅读请进入《Delphi》频道 >>