Android Question Phone library, Contacts2 object, GetAll method takes too long

MoraviaVenus

Member
Licensed User
Longtime User
Hi all,

I face a problem with method GetAll of Contacts2 object, library Phone.

My app loads the list of all names and phone numbers from phone. I use following code:
B4X:
Dim allContacts As Contacts2
Dim listOfAllContacts As List

listOfAllContacts = allContacts.GetAll(True, False)

Problem is, that in my physical phone, there are roughly 500 phone numbers and this line of code takes 12 seconds to execute. During that time, telephone displays message "Sorry ! Activity is not responding. Force close / Wait", which botheres users. Good thing is, that after clicking Wait, application finishes loading of contacts and everything goes on.

I tried to put
B4X:
DoEvents
ProgressDialogShow("...")
DoEvents
before .GetAll method and
B4X:
DoEvents
ProgressDialogHide
after .GetAll, so users know what is going on, but "sand clock" froze suddenly and in some seconds I got message "Sorry ! Activity is not responding. Force close / Wait" even while ProgressDialogShow was visible. It seems to me, that method .GetAll freezes also ProgressDialogShow process.

Please, could you advise me how to either speed up .GetAll method or keep user somehow informed, that he/she should be patient and wait longer?

Thank you.
 

ddahan

Member
Licensed User
Longtime User
I'm using ContactsUtils class, it is much faster.
Today, I have added additional methods the covers almost all contact fields. you can find the updates here.
Hope it helps.

David.
 
Upvote 0

MoraviaVenus

Member
Licensed User
Longtime User
Hi David,

thank you for suggestion. Could you please write me few lines of code, how can I get simple list with names and telephone numbers using your ContactsUtils class? I need something like:

John Smith: 00435987346207
John Smith: +43583084332
Hans Maier: 024568234
Andrea Maier: 055456732
...

Thanks.
 
Upvote 0

MoraviaVenus

Member
Licensed User
Longtime User
Hi Erel,

thank you.

The code I prepared based on the tutorial is faster, but still takes 6 seconds, what can make users confused (ProgressDialogShow does not appear despite the fact, that is written in code).

B4X:
ProgressDialogShow("Loading contacts...")
   
Dim singleContactPhones, phoneNumber As String
Dim allContacts As List = cu.FindAllContacts(True)
   
allContacts.SortType("DisplayName", True)
For Each c As cuContact In allContacts
    Dim contactPhones As List = cu.GetPhones(c.ID)
    'begin of my stupid code
    For n = 0 To contactPhones.Size - 1
        singleContactPhones = contactPhones.Get(n)
        phoneNumber = singleContactPhones.SubString2(singleContactPhones.IndexOf("Number=") + 7, singleContactPhones.IndexOf(", IsInitialized="))
        lstvContacts.AddSingleLine(c.DisplayName & ":  " & phoneNumber)
    Next
    'end of my stupid code
Next

ProgressDialogHide

Could someone of you please suggest me more elegant (more effective) way how to get list of all names and related phone numbers?

Thank you.
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
This will be done in 1 - 2 seconds (or even less, with 700 contacts) with ContentResolver.

B4X:
Sub GetAllContacts
  '//Private cr As ContentResolver (you may need to declare it in Sub "Globals"...
    Dim phonesUri As Uri
    phonesUri.Parse("content://com.android.contacts/data/phones")

    Dim phones As Cursor = cr.Query(phonesUri, Array As String("data4", "display_name"),"", Null, "display_name")
    For p = 0 To phones.RowCount - 1 '// need to make sure you have at least 1 contact
        phones.Position = p
        Log(phones.GetString("display_name") & " " & phones.GetString("data4"))
    Next
    phones.Close
End Sub

Check the logs, all contacts should be there.
 
Upvote 0

MoraviaVenus

Member
Licensed User
Longtime User
Hi bsnqt,

amazing, on my old-school device (Android 2.1) it takes less than 2 seconds to load all 500 phone numbers, thank you.

But, strange is, that the phone numbers are displayed in reverse order. For example, when one of my contact has phone number 11223344, your code returns phone number 44332211 :) What is the root cause of this behavior? Little and big endians? How can I avoid it?

Thank you
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
Hi bsnqt,

amazing, on my old-school device (Android 2.1) it takes less than 2 seconds to load all 500 phone numbers, thank you.

But, strange is, that the phone numbers are displayed in reverse order. For example, when one of my contact has phone number 11223344, your code returns phone number 44332211 :) What is the root cause of this behavior? Little and big endians? How can I avoid it?

Thank you

Glad to hear it works.

Ref to the reverse order of contact phone number string, I don't experience such issue. Actually if you look to my codes, the string is just simply read directly from what saved in your phone, i.e the contacts database. Do you see that all of your contacts having same "reverse" issue or only some of them?

I will look again on this tonight.
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
Test and confirmed, the code runs well, I got 800 contacts in less than a second (Xperia Z2) without any "reverse order" issue of string.
 
Upvote 0
Top