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

Status
Not open for further replies.

CapReed

Member
Licensed User
Longtime 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:

Douglas Farias

Expert
Licensed User
Longtime 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
 

Douglas Farias

Expert
Licensed User
Longtime User
i need change number of contacts selected
 

Beja

Expert
Licensed User
Longtime 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.
 

fbritop

Active Member
Licensed User
Longtime User
Erel,
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

B4X:
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
 

Dwight Schrute

New Member
Licensed User
Longtime User
Hello,

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:

B4X:
SELECT LastName, FirstName, NotificationSound,
NotificationSoundFilePath, NotificationSoundBlob
FROM Contacts
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 --
DKS,
Assistant to the Assistant to the Regional Manager
 
Last edited:

JOTHA

Well-Known Member
Licensed User
Longtime User
Added some new functions to get ContactGroups, and filter contacts by Contact Groups.

How can I get the Groups of one Contact?

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

Erel

B4X founder
Staff member
Licensed User
Longtime User
What is the code of FindAllGroups?

This code will get the group ids:
B4X:
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(uri, Array As String("group_sourceid"), "", Null, "")
   Dim group_sources As List
   group_sources.Initialize
   For i = 0 To c.RowCount - 1
     c.Position = i
     If c.GetString("group_sourceid") <> Null Then
       group_sources.Add(c.GetString("group_sourceid"))
     End If
   Next
   c.Close
   Return group_sources
End Sub
 

JOTHA

Well-Known Member
Licensed User
Longtime User
Hi Erel,

thank you for responding!

The code of FindAllGroups (Code by thedesolatesoul) is as follows:
B4X:
Public Sub FindAllGroups As List
  
    Dim Projection(2) As String = Array As String("_id", "title")
    Dim crsr As Cursor = cr.Query(groupContactUri, Projection,   "title <> ?", Array As String("null"), "")
    Dim ret As List
    ret.Initialize
    For i = 0 To crsr.RowCount - 1
        crsr.Position = i
        Dim Grp As cuGroup
        Grp.Id    = crsr.GetLong("_id")      
        Grp.Title = crsr.GetString("title")
        ret.Add(Grp)
    Next
    crsr.Close
    Return ret
  
End Sub

When I use your code (GetGroups)
B4X:
Log("-GetGroups: "&ContactsUtils1.GetGroups(Kontakt.Id)&"")
the result is:
GetGroups: (ArrayList) [6, 6f3ff5dc8e50c0ab, 2ae8b8430881e18f, 786a3cd288035f2f, 6f3ff5dc8e50c0ab, 6f3ff5dc8e50c0ab]

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.
 

DonManfred

Expert
Licensed User
Longtime 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
 

JOTHA

Well-Known Member
Licensed User
Longtime 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 ...
 

JOTHA

Well-Known Member
Licensed User
Longtime User
V1.20 is attached to the first post. It includes a GetGroups methods that returns the contact's group names.
Thank you Erel!
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…