delphi Android实例-读取设备联系人(XE8+小米2)  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi Android实例-读取设备联系人(XE8+小米2)


结果:
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;

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.

来源:https://www.cnblogs.com/FKdelphi/p/4816457.html

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.40742087364197 seconds