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

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

相关资料:

http://www.colabug.com/thread-1071065-1-1.html

结果:

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<JString>;
oselectionArgs: TJavaObjectArray<JString>;
FieldIndex: Integer;
begin
if AList <> nil then
AList.Clear;
oprojection := nil;
oselectionArgs := nil;
if AName.Length > then
begin
oprojection := TJavaObjectArray<JString>.Create();
oselectionArgs := TJavaObjectArray<JString>.Create();
oprojection.Items[] := 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.
上一篇:应用Druid监控SQL语句的执行情况


下一篇:C C++ OC iOS面试重点问题(一)