5

How can I get a phone's contact list in a FireMonkey mobile application?

2
  • 1
    Are you tried reading the documentation of android and iOS about this topic?
    – RRUZ
    Nov 8, 2013 at 20:58
  • I am not writing in java or object c , I am using Rad Studio XE5. Nov 9, 2013 at 12:05

3 Answers 3

3

here you go .. It's not finished as it reads all numbers for one person and if there are two numbers you will have two times this person listed inside list .. but from here I think you can work and adjust it to your needs :))

function GetContact: TStringList;
var
cursorContacts, cursorContactsPhone: JCursor;
hasPhoneNumber: Integer;
id: Int64;
displayName, phoneNumber, contactID: string;
begin
Result := TStringList.Create;
cursorContacts := SharedActivity.getContentResolver.query(TJContactsContract_Contacts.JavaClass.CONTENT_URI, nil, nil, nil, nil);
if (cursorContacts.getCount > 0) then
begin
while (cursorContacts.moveToNext) do
begin
id := cursorContacts.getLong(cursorContacts.getColumnIndex(StringToJString('_ID')));
displayName := JStringToString(cursorContacts.getString(cursorContacts.getColumnIndex(StringToJString('DISPLAY_NAME'))));
hasPhoneNumber := cursorContacts.getInt(cursorContacts.getColumnIndex(StringToJString('HAS_PHONE_NUMBER')));
if (hasPhoneNumber > 0) then
begin
cursorContactsPhone := SharedActivity.getContentResolver.query(TJCommonDataKinds_Phone.JavaClass.CONTENT_URI, nil,StringToJString('CONTACT_ID = ' + IntToStr(id)),nil, nil);
while (cursorContactsPhone.moveToNext) do
begin
phoneNumber := JStringToString(cursorContactsPhone.getString(cursorContactsPhone.getColumnIndex(StringToJString('DATA1'))));
contactID := JStringToString(cursorContactsPhone.getString(cursorContactsPhone.getColumnIndex(StringToJString('CONTACT_ID'))));
Result.Add(displayName + ': ' + phoneNumber);
end;
cursorContactsPhone.close;
end;
end;
end;
cursorContacts.close;
end;

Best Regards, Kruno

9
  • Hey, I'm having issues using this code. I get Undeclared identifier for TJContactsContract_Contacts.JavaClass.CONTENT_URI and TJCommonDataKinds_Phone.JavaClass.CONTENT_URI. Do you happen to know why, and how to solve this? Thanks!
    – That Marc
    Jan 20, 2014 at 0:08
  • 1
    you need Androidapi.Jni.Provider in uses for this and put in uses also this: Androidapi.JNIBridge, Androidapi.Jni.GraphicsContentViewText, Androidapi.Jni.Net,Androidapi.Jni.JavaTypes, FMX.Helpers.Android ...
    – mali kruno
    Jan 21, 2014 at 19:52
  • A ha! The .Provider was missing! :O I already have all the others, thanks. Tried to include several others, but didn't get to Provider as it seems :/ Thanks!!! Ps: Do you happen to have newer/updated version of this code, which also removes the duplicates of person's appearance etc, or didn't do any further progress at this one? :)
    – That Marc
    Jan 21, 2014 at 23:28
  • sorry for late replay :) ... i will post here the solution for filtering but I don't have it on this station from where I'm posting ... will do it asap but you can do it also : first get all names and ID's into one list and in second list based on names get the number related to name and PhoneID :)
    – mali kruno
    Jan 30, 2014 at 18:42
  • I actually already got it working. :) Thanks anyway!! I sort it, so I have no duplicates, I also got it working for auto-search with tEdit.OnChange event by querying with "LIKE" attribute through names, though it works very slow, if I did it with one letter only, so I work with at least 2 characters now, and it works flawlessly :)
    – That Marc
    Jan 31, 2014 at 1:52
1

Here's my code (inspired and originally created by @mali kruno, I only changed it to my needs!) to search through all contacts based on TEdit OnChange event:

I use this function in my commonfunctions.pas unit:

function GetContact (Name: string; Number: string; const tip: integer) : TStringList;
var
cursorContactsPhone: JCursor;
Typo1, Typo2: string;
FindBy: JString;
ToFind: TJavaObjectArray<JString>;
CurRec: integer;
begin
Result:=TStringList.Create;
CurRec:=0;
ToFind:= TJavaObjectArray<JString>.Create(2);
if Name <> '' then
  begin
    ToFind.Items[0] := StringToJString('data1');
    ToFind.Items[1] := StringToJString('display_name');
    FindBy := StringToJString('display_name LIKE "%' + Name + '%"');
    Typo1:='data1';
    Typo2:='display_name';
  end
else if Number <> '' then
  begin
    ToFind.Items[0] := StringToJString('display_name');
    ToFind.Items[1] := StringToJString('data1');
    FindBy := StringToJString('data1 LIKE "%' + Number + '%"');
    Typo1:='display_name';
    Typo2:='data1';
  end;
cursorContactsPhone := SharedActivity.getContentResolver.query(TJCommonDataKinds_Phone.JavaClass.CONTENT_URI, ToFind, FindBy, nil, nil);
while (cursorContactsPhone.moveToNext) do
begin
Result.Add
(JStringToString(cursorContactsPhone.getString(cursorContactsPhone.getColumnIndex(StringToJString(Typo2)))) + ' - ' +
JStringToString(cursorContactsPhone.getString(cursorContactsPhone.getColumnIndex(StringToJString(Typo1)))));
CurRec:=CurRec+1;
end;
cursorContactsPhone.close;
end;

I call it from ContactSearch.Change event (it's TEdit component) like this:

procedure TMainF.ContactsSearch.Change(Sender: TObject);
var ResultNo: integer; SearchContacts: string; Results: TStringList;
begin                   // begin main procedure
if ContactsSearch.Text.Length > 1 then
begin                 //begin search and memo update
SearchContacts:=ContactsSearch.Text;
Results:=GetContact(SearchContacts, '', 0);
ResultNo:=0;
Memo1.Lines.Clear;
for ResultNo := 0 to Results.Count-1
do
begin
Memo1.Lines.Add(Results.Strings[ResultNo]);
end;

Results.Free;
end;
end;

Note, that the Result is a TStringList created in a function and freed in a procedure after Memo update. Note also, that I only search if TEdit length is 2 or more, since otherwise entering just "a" in a tedit would show all contacts that have a letter "a" in their name, and therefore it would freeze a little every time you search, use backspace etc... The workaround would be to load the phonebook in a TStringList on application start, and then search through the stringlist only, but that would make few other troubles: a) phonebook update wouldn't be detected, or you'd have to implement "Update" button, which would make no sense to do the workaround at all.. b) app start would take longer c) haven't tried that and not sure how much would it actually speed-up the search, since the Memo.Lines.Add takes more time than the query itself, so...

As for the duplicates, you can see that here are not handled, because currently I don't have a need to do so, but you can easily handle this using "sort" in a Memo, or, even better if you don't want to lose the entries that would otherwise appear as a duplicate, manage them inside a TStringList itself, so that you merge numbers in the same line, or create sub-stringlists for each name (of course, only if a name appears more than once, if you don't want to end up having twice as much stringlists as you'd actually need).

Hope this helps.

1
  • This example will not work at 100%, because the item 'data1' is used to display the phone number on the display (on some models of devices are used to display the number, for example spaces or dashes). It is better to use the item 'data4' because it uses raw data.
    – pudnivec74
    Sep 5, 2015 at 20:18
0

You do it in much the same way as a programmer would who uses the native programming APIs, given that Delphi does not provide a unified/wrapped solution to this problem.

You need to research how the Android SDK surfaces the contact list and how the iOS SDK surfaces its contact list, then make use of the native APIs to access it.

It will differ wildly between the 2 platforms, but it would be feaible to write some OS-independent interface to it once you've established the implementation on the 2 different OSs and seen what is on offer and what is accessible across the two implementations. This is what FMX does in other instances of similar features implemented on the two platforms.

If the required APIs haven't already been imported into Delphi's RTL, which is quite possible, then you'd also need to write the imports for those APIs you need in order to be able to call them in the first place.

Executive summary:

  1. Roll up your sleeves
  2. Get stuck in
  3. Code it up yourself
  4. Bask in the pleasure of having got some cool API stuff working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.