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

Status
Not open for further replies.
ContactsUtils is a class based on ContentResolver library.

ContactsUtils allows you to read all kinds of information from the device stored contacts and also to modify the information, add new contacts and delete existing contacts.

Most of the methods expect a contact id. You can get this id by calling one of the Find methods. All the Find methods return a List that holds cuContact items.

The cuContact object holds the contact name and id. With that id you can get more information, add or modify existing fields and delete the contact.

Types:
B4X:
Type cuContact (Id As Long, DisplayName As String)
Type cuEmail (Email As String, EmailType As String)
Type cuPhone (Number As String, PhoneType As String)
Type cuEvent (DateString As String, EventType As String)

Public subs:
B4X:
'Returns a List with cuContact items based on the given name.
'Name - Name to look for.
'Exact - Whether to search for the exact name or to search for names that contain the given value.
'VisibleOnly - Whether to return only visible contacts. In most cases there are many invisible contacts.
Public Sub FindContactsByName(Name As String, Exact As Boolean, VisibleOnly As Boolean) As List

'Similar to FindContactsByName. Finds contacts based on the mail address.
Public Sub FindContactsByMail(Mail As String, Exact As Boolean, VisibleOnly As Boolean) As List

'Similar to FindContactsByName. Finds contacts based on the notes field.
Public Sub FindContactsByNotes(Note As String, Exact As Boolean, VisibleOnly As Boolean) As List

'Similar to FindContactsByName. Finds contacts based on the phone number.
Public Sub FindContactsByPhone(PhoneNumber As String, Exact As Boolean, VisibleOnly As Boolean) As List

'Returns the starred contacts.
Public Sub FindContactsByStarred(Starred As Boolean) As List

'Returns all contacts.
Public Sub FindAllContacts(VisibleOnly As Boolean) As List

'Returns all contacts with a photo.
Public Sub FindContactsWithPhotos As List

'Returns a List with cuEmail items.
Public Sub GetEmails(Id As Long) As List

'Returns a List with cuEvents items.
Public Sub GetEvents(Id As Long) As List

'Returns a List with cuPhone items.
Public Sub GetPhones(id As Long) As List

'Returns the note field.
Public Sub GetNote(id As Long) As String

'Returns the thumbnail photo of the given contact. Returns an uninitialized bitmap if no photo is available.
Public Sub GetPhoto(Id As Long) As Bitmap

'Gets whether the contact is "starred".
Public Sub GetStarred(Id As Long) As Boolean

'Sets the note field of the given id.
Public Sub SetNote(Id As Long, Note As String)

'Adds an email field to the given contact id.
'EmailType - One of the email types strings (see Initialize method).
Public Sub AddEmail(Id As Long, Email As String, EmailType As String)

'Adds a phone field to the given contact id.
'PhoneType - One of the phone types strings (see Initialize method).
Public Sub AddPhone(Id As Long, PhoneNumber As String, PhoneType As String)

'Deletes the given phone number.
Public Sub DeletePhone(Id As Long, PhoneNumber As String)

'Deletes the given email address.
Public Sub DeleteEmail(Id As Long,Email As String)

'Sets the starred state of the given id.
Public Sub SetStarred (Id As Long, Starred As Boolean)

'Deletes the contact with the given Id.
Public Sub DeleteContact(Id As Long)

'Inserts a new contact and returns the cuContact object of this contact.
Public Sub InsertContact(Name As String, Phone As String) As cuContact

To use this class you need to add a reference to the ContentResolver and SQL libraries.

The following permissions should be added in the manifest editor:
B4X:
AddPermission("android.permission.READ_CONTACTS")
AddPermission("android.permission.WRITE_CONTACTS") 'if write access is required

It should be pretty simple to extend this class and retrieve or modify other fields based on the existing code.

The attached project, which includes the ContactsUtils class shows a list with the visible contacts. Pressing on a contact will show some details about the contact and its photo. Long pressing on a contact will change its starred state.

V1.20 - Adds GetGroups and GetAccounts methods.
V1.10 - Adds GetEvents method.
V1.05 - Fixes an issue with InsertContact on HTC devices.
 

Attachments

  • ContactsUtils.zip
    11.6 KB · Views: 1,595
Last edited:

hzytsoft

Member
Licensed User
Longtime User
hi Erel


i use this code
Dim c As cuContact

c=cu.InsertContact("jim","1234567890")
ToastMessageShow(c.Id, False)
cu.AddPhone(c.Id,"87909877","home")
but got wrong
then i use this code
cu.AddPhone(c.Id,"87909877","1")

also got wrong
===============
logs show:
java.lang.IllegalArgumentException: Invalid column name_raw_contact_id

pls
 

hzytsoft

Member
Licensed User
Longtime User
B4X:
Sub Button1_Click
Dim c As cuContact

  c=cu.InsertContact("jim","1234567890")
  ToastMessageShow(c.Id, False)
  cu.AddPhone(c.Id,"87909877","home")
End Sub
 

shaffnert03

Member
Licensed User
Longtime User
Don't mean to jump in front of hzytsoft's question, but have one for after his is answered.

Trying to use the new getevents() function (thanks so much for adding that BTW!), and having some issues getting the information out. If I put the result into a cuEvents object I can get data out when it's populated, but if the contact has no event data I can't seem to check for that.

IsInitialized doesn't work since I had to initialize the variable before pulling it (didn't work if I tried not to), and if try to check and see if "EventType" is <> "" then the program bombs out. Log shows "Error occurred on line: 118 (contactsutils) importcontactsafterFirstLayout (java line: 98)"

As an alternative I tried to put the getevents result in a normal list. I could then put the items from that into an object, but nothing I put in the get function on the object seemed to get the data out.

Any help with this? Here's a few lines that I'm trying as a visual:

B4X:
Dim EventList As cuEvent
    EventList.Initialize
EventList = cu.GetEvents(Contact.Id)
    If EventList.EventType = "birthday" Then lbl2.Text = EventList.DateString

Also, more generally, this is like the third or fourth question I've had on just getting data out of various data types, is there somewhere particular you'd recommend I go for more information on this? I've got both user manuals and the Kindle book on B4A, anywhere in particular I should look to understand this better?
 

HARRY

Active Member
Licensed User
Longtime User
Hi,

I use this class with pleasure, it works very well. However, it does not do all things I need. I have to extract the postal address of a contact from which I know the iD.

In the first post Erel says:It should be pretty simple to extend this class and retrieve or modify other fields based on the existing code.

For me, however, it is not "pretty simple"; whatever I try, based on studying various sources on internet, I do not reach my goal.

Can somebody give me some help?


Harry
 

HARRY

Active Member
Licensed User
Longtime User
Hi Erel,

Thanks very much. It works without problems. I was looking in the same direction, but did not know how to handle raw.

Harry
 

aklisiewicz

Active Member
Licensed User
Longtime User
Using Android 4.3+
I would like to know how can I use this LIB to display Contacts in my Application based on the selected contact group.
I do not see any GROUP property in the lib referenced...

I would like to have a SIPNNER which allows the user to select a GROUP, then use ListView (or alike) to display contacts based on that group.

Arthur
 

derez

Expert
Licensed User
Longtime User
The names of the contacts are cut when their length exceeds ~12-14 characters. It comes like this from the cr.Query - crsr.GetString("display_name").
Any reason for this ?
 

derez

Expert
Licensed User
Longtime User
You can try to fetch the name from this uri: vnd.android.cursor.item/name
I get the same thing with the above , both on nexus 5 and samsung 2.
 

derez

Expert
Licensed User
Longtime User
I guess the problem is from transfering the contacts to and from sim cards. I'll check later with a new contact.
Edit: it is like I wrote above. If I update the contact it shows in full.
 
Last edited:

aklisiewicz

Active Member
Licensed User
Longtime User
Note that this is not a library, it is a class module.

ContactsUtils currently doesn't provide access to contacts groups.


any chance to get groups support in a near future ?
Is there any other way to filter through groups ?
Art
 

Gennaro Frungillo

Member
Licensed User
Longtime User
Hi Erel, i've a question:
it's possible to load all contact into a listview inside an async operation?
Let me explain, I need to load all the address book contacts in a listview with their images, but the operation takes enough time, it would be nice to load the contacts in the list asynchronously in order to give the user the possibility of immediate access to the form that contains the listview, without having to wait for loading of all contacts.
With visual studio I used the component BackGroundWorker or threading class.
 

CapReed

Member
Licensed User
Longtime User
I have spent several hours this item, It is very rare ...

I added this sub to update the contact name.

B4X:
Public Sub PonNombre(Id As Long, Name As String)
    Dim v As ContentValues
    v.Initialize
    v.PutString("data1", Name)
    SetData("vnd.android.cursor.item/name", v, Id, True)
End Sub

Works fine, but there is one detail that I do not understand.
Suppose that the contact old name was MiNameIs Anthony
Suppose that the contact new name is Ama
Well, the result after executing the subroutine is that Ama Anthony appears in the address book :confused:
I do not understand ...

Can you help me????
 
Status
Not open for further replies.
Top