Delphi XE6 读取Android设备联系人


本文整理自网络,侵删。

 介绍

本文章介绍了Delphi XE6读取Android设备联系人,在编写代码之前,首先需要添加读取联系人的权限,然后引用需要的单元
uses
  FMX.Helpers.Android,
  Androidapi.JNI.JavaTypes, Androidapi.JNI.GraphicsContentViewText,
  FMX.Platform.Android, Androidapi.JNIBridge, Androidapi.JNI.Provider,
  Androidapi.Helpers;


编写检索联系人的函数代码
procedure QueryContact(AName: string; AList: TStrings);
var
  cursorContactsPhone: JCursor;
  selection: string;
  projection: TJavaObjectArray<JString>;
  FieldIndex: Integer;
begin
  if AList <> nil then
    AList.Clear;
  projection := nil;
  if AName.Length > 0 then
  begin
    projection := TJavaObjectArray<JString>.Create(1);
    projection.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,
    projection {要查询的字段名,nil的全部},
    StringToJString(selection){Where条件},
    nil { 这里是Where语句的条件参数们,我上面图方便,写死在Where条件中了,没使用参数 } , nil);
  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;

函数的参数:1是需要查询人的名称,可以是名字中的其中一个字 2查询得到的返回结果,是姓名的列表

函数写好后,可以通过调用这个函数来读取联系人,调用的代码如下:
procedure TForm2.Button1Click(Sender: TObject);
var
  s: TStrings;
begin
  s := TStringList.Create;
  QueryContact('苏', s); //查询姓苏的人
  ShowMessage(s.Text);
  s.Free;
end;

相关阅读 >>

使用indy解决base64回车换行问题

Delphi二值图像投影算法

Delphi安卓动态切换本地主题

rightstr 返回字符串右边指定个数的新字符(串)

Delphi 日期时间函数

Delphi tms web core messagedlg对话框用法

Delphi 字符串保存utf-8过程

Delphi中内嵌汇编

Delphi win10下message无法接收的问题

Delphi 瞬间消除无用托盘图标(刷新托盘)

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...