B4A Library [class] ContactsUtils - Provides read / write access to the stored contacts

Discussion in 'Additional libraries, classes and official updates' started by Erel, Jul 7, 2013.

  1. Erel

    Erel Administrator Staff Member Licensed User

    The name structure is quite complicated. It will be probably simpler to delete the contact and create a new one with the new name.
  2. CapReed

    CapReed Member Licensed User

    But that would imply that if the contact has picture, notes, phones, emails, etc, etc, would be lost to delete and re-create.

    I think there must be a way to resolve this without having to delete / create.

    The problem arises mainly when there are already some other contact with the same name to be modified. I'm still investigating ...

    If you think of anything, would be appreciated.
    Last edited: Feb 28, 2014
  3. Douglas Farias

    Douglas Farias Expert Licensed User

    @Erel how can i edit one contact with id? i dont found this on the forum only get all contacts but not edit with id *-*
    i need delet and add new? or have a way to only edit? i dont find any set, edit function
  4. Erel

    Erel Administrator Staff Member Licensed User

    Which field do you want to update?
  5. Douglas Farias

    Douglas Farias Expert Licensed User

    i need change number of contacts selected
  6. Erel

    Erel Administrator Staff Member Licensed User

    You can use DeletePhone and then add a new phone with AddPhone (if you don't see these methods then I need to upload a new version of ContactsUtils).
  7. Beja

    Beja Expert Licensed User

    Hi Erel,
    I read the first page of this thread, so the answer to my question may be buried somewhere inside..
    I am asking if with this lib, I can copy all contacts and then add them to another phone? A simple code is much appreciated.
  8. Erel

    Erel Administrator Staff Member Licensed User

    Yes, you can. You might lose some information like contacts groups. However it shouldn't be difficult to copy the basic information and add it to a new phone.
  9. fbritop

    fbritop Active Member Licensed User

    One thing I came across when searching for a contact by his phone number, is that Android, somehow formats the phone number in the contact data store, depending on the country with some spaces. For example, my phone number 5699129XXXX gets stored as 56 9 9129 XXXX.

    So when I try to search for 9129XXXX, because of the strange spacing, it does not return the matched contact. I ended up viewing the contact data format on Android devices, and the raw main number is stored in "data4". I'm not sure if this is right, but at least it searchs and matches now the phone number that I'm looking for in the contacts.

    It helps to search more deeply with the folowwing Sub

    Public Sub FindContactsByPhone2(PhoneNumber As String, Exact As Boolean, VisibleOnly As Boolean) As List
    Return FindContactsIdFromData("vnd.android.cursor.item/phone_v2""data4", PhoneNumber, "=", Exact, VisibleOnly)
    End Sub
    Claudio Oliveira likes this.
  10. Dwight Schrute

    Dwight Schrute New Member Licensed User


    I am being lazy here, but I have a very specific task and just figured it would be easier to ask IF I can do it before I invested a lot of time in learning the ContactUtils functions.

    I have assigned several of my contacts a custom notification sound for when I receive a text from them. I've done this too many times, tho, and now I'm forgetting who got a custom notification sound, and if they did, what does it sound like.

    I'll want to be able to:

    1. Loop through all my contacts.
    2. Perform the following Pseudocode:

    SELECT LastName, FirstName, NotificationSound,
    NotificationSoundFilePath, NotificationSoundBlob
    WHERE NotificationSound 
    IS NOT NULL and NotificationSound <> "Default"
    All the fields returned in such a query would be strings except for the NotificationSoundBlob, which would be the actual sound in binary.

    BTW, NotificationSound is not a standard field the user is presented with when adding a new contact; you need to select "Add Another Field" and selecting it from there.

    I don't know in what Android version the ability to add customized notification sounds for individual contacts was added.

    Thanks --
    Assistant to the Assistant to the Regional Manager
    Last edited: Jul 24, 2016
  11. Erel

    Erel Administrator Staff Member Licensed User

    ContactsUtils doesn't include a method to get the notification sound assigned to specific contacts.
  12. JOTHA

    JOTHA Well-Known Member Licensed User

    How can I get the Groups of one Contact?

    If I code this ... I'll get ALL Groups listed.
    For Each Grp As cuGroup In ContactsUtils1.FindAllGroups()
    ""&Grp.Id&": "&Grp.Title&"").Append(CRLF)
  13. Erel

    Erel Administrator Staff Member Licensed User

    What is the code of FindAllGroups?

    This code will get the group ids:
    Public Sub GetGroups(Id As Long) As List
    Dim uri As Uri
    uri.Parse("content://com.android.contacts/contacts/" & Id & "/entities")
    Dim c As Cursor = cr.Query(uriArray As String("group_sourceid"), ""Null"")
    Dim group_sources As List
    For i = 0 To c.RowCount - 1
         c.Position = i
    If c.GetString("group_sourceid") <> Null Then
    End If
    Return group_sources
    End Sub
  14. JOTHA

    JOTHA Well-Known Member Licensed User

    Hi Erel,

    thank you for responding!

    The code of FindAllGroups (Code by thedesolatesoul) is as follows:
    Public Sub FindAllGroups As List
    Dim Projection(2As String = Array As String("_id""title")
    Dim crsr As Cursor = cr.Query(groupContactUri, Projection,   "title <> ?"Array As String("null"), "")
    Dim ret As List
    For i = 0 To crsr.RowCount - 1
            crsr.Position = i
    Dim Grp As cuGroup
            Grp.Id    = crsr.GetLong(
            Grp.Title = crsr.GetString(
    Return ret
    End Sub
    When I use your code (GetGroups)
    Log("-GetGroups: "&ContactsUtils1.GetGroups(Kontakt.Id)&"")
    the result is:
    I think these are objects, but I need a list of all Groups for 1 Contact (Kontakt.Id)
    I can't convert them into String.
  15. DonManfred

    DonManfred Expert Licensed User

    You now have an Array with pairs... Index and id of the Group for this contact.

    Remember the id's in a new list.
    Check all Groups and compare the groupid with the remembered id's. If a Group Match you then have the name for this Group...

    I did not tried it but as from the code in this thread i guess it should work
  16. JOTHA

    JOTHA Well-Known Member Licensed User

    Hello DonManfred,

    with FindAllGroups I can generate a List of all Groups (included Id's), but with GetGroups I can't generate such a List ...
  17. Erel

    Erel Administrator Staff Member Licensed User

    What is the value of groupContactUri?
  18. JOTHA

    JOTHA Well-Known Member Licensed User

    Hello Erel,
    The Value is: (StringUri) content://com.android.contacts/groups
  19. Erel

    Erel Administrator Staff Member Licensed User

    V1.20 is attached to the first post. It includes a GetGroups methods that returns the contact's group names.
    JOTHA likes this.
  20. JOTHA

    JOTHA Well-Known Member Licensed User

    Thank you Erel!
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice