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,556
Last edited:

JOTHA

Well-Known Member
Licensed User
Longtime User
Is it possible to get really every information out of my contacts (f.e: spouse, etc.)?
 

JOTHA

Well-Known Member
Licensed User
Longtime User
Thank you Erel ...
 

JOTHA

Well-Known Member
Licensed User
Longtime User
... another problem:
I want to get the first 4 phone numbers from a contact to write them in 4 fields.
The code to get these 4 phone numbers ...
B4X:
For Each phone As cuPhone In ContactsUtils1.GetPhones(Kontakt.Id)
        Dim Kontakte_Telefon_1,  Kontakte_Telefon_2, Kontakte_Telefon_3, Kontakte_Telefon_4 As String
        Kontakte_Telefon_1 = ContactsUtils1.GetPhones(Kontakt.Id).Get(0)
        Kontakte_Telefon_2 = ContactsUtils1.GetPhones(Kontakt.Id).Get(1)
        Kontakte_Telefon_3 = ContactsUtils1.GetPhones(Kontakt.Id).Get(2)
        Kontakte_Telefon_4 = ContactsUtils1.GetPhones(Kontakt.Id).Get(3)
            Log(""&phone.Number&"")
    Next
The result is not the solution which I'm looking for ...
Only in the Log I can get the telephone numbers, but how can I get a result that looks like this:

Kontakte_Telefon_1 = PhoneNumber_1
Kontakte_Telefon_2 = PhoneNumber_2
Kontakte_Telefon_3 = PhoneNumber_3
Kontakte_Telefon_4 = PhoneNumber_4

Any idea?
 

desof

Well-Known Member
Licensed User
Longtime User
ES: Y como es posible agregar un nuevo contacto ?
EN: And how is it possible to add a new contact?
 

beacon

Member
Licensed User
Longtime User
B4A: I can add a new contact to the phone's contacts list successfully with ContactUtils and ContentResolver Ver 1.50.

Is there please a method to add a photograph to a contact?

Thank you.
 

Dogbonesix

Active Member
Licensed User
Longtime User
Is there a way to edit the display name? Especially helpful for typing error. It is reasonably easy to change everything else except the display name.
 

stanks

Active Member
Licensed User
Longtime User
hm i have a permission problem. yesterday my app worked fine but i have upgraded my s7 with official oreo (v8) now i get error msg
B4X:
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{...} (pid=21476, uid=10403) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
and i have that 2 permission in manifest
B4X:
AddPermission(android.permission.READ_CONTACTS)
AddPermission(android.permission.WRITE_CONTACTS) 'if write access is required

i treid with uninstall "old" version of my app from device then new install but failure too

any idea?

thanks
 

DonManfred

Expert
Licensed User
Longtime User
hm i have a permission problem
You should start a NEW thread for your Issue. It is probably not related to this tutorial.
This two Permissions are Dangerous Permissions.
frc089.png


TargetSDK
Especially you should check RuntimePermissions.
 

Zlgo

Member
I try this example and in compiling (debug mode) receive this message!
B4X:
B4A Version: 9.80
Java Version: 8
Parsing code.    (0.04s)
Building folders structure.    (0.02s)
Compiling code.    (0.09s)
Compiling layouts code.    (0.00s)
Organizing libraries.    (0.00s)
    (AndroidX SDK)
Generating R file.    (0.01s)
Compiling debugger engine code.    (0.00s)
Compiling generated Java code.    (0.01s)
Convert byte code - optimized dex.    (2.07s)
Packaging files.    (0.60s)
Copying libraries resources    (0.00s)
Signing package file (debug key).    (0.97s)
ZipAlign file.    (0.05s)
Installing file to device.    Error
Performing Push Install
ContactsUtils_RAPID_DEBUG.apk: 1 file pushed, 0 skipped. 2.3 MB/s (1052341 bytes in 0.434s)
    pkg: /data/local/tmp/ContactsUtils_RAPID_DEBUG.apk
Failure [INSTALL_FAILED_PERMISSION_MODEL_DOWNGRADE]
 

Zlgo

Member
I have try again this example(ContactuUtils) with B4A 10. and after installation receive same error like member Stanks
B4X:
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2ForLG from ProcessRecord{2a1aff1 9437:b4a.example/u0a139} (pid=9437, uid=10139) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
In manifest is already this two permissions and Sdk = 28
 
Status
Not open for further replies.
Top