delphi读取Android设备联系人.DelphiXE6中编译通过  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi读取Android设备联系人.DelphiXE6中编译通过


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;
  FieldIndex: Integer;
begin
  if AList <> nil then
    AList.Clear;
  projection := nil;
  if AName.Length > 0 then
  begin
    projection := TJavaObjectArray.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;
 
procedure TForm2.Button1Click(Sender: TObject);
var
  s: TStrings;
begin
  s := TStringList.Create;
  QueryContact('王', s); //查询姓王的人
  ShowMessage(s.Text);
  s.Free;
end;

 

来源:http://www.raysoftware.cn/?p=501


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

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

执行时间: 0.037530899047852 seconds