Solution: Create Contact

optimist

Member
Licensed User
Longtime User
Hello,
the b4a phone library contains read only access to the contacts.
but how about writable access? people often asked, but statement here was that it is not possible.

But after a little bit research in this forum i mentioned that b4a has a very good support for external libraries. It seems, that almost all lower functions are loaded from an external library.

So i wrote an library (the first in my live ;) ). Using it is is really easy:

B4X:
Sub Button1_Click

  'Add libs phone and my miscUtil
  Dim mu As miscUtil, c As Contact
  mu.Initialize
  Dim name As String, phoneNr As String, phoneType As Int, mail As String, mailType As Int
  'define contact data
  name = "Mike Tester"
  phoneNr = "004912345678"
  phoneType = c.PHONE_HOME
  mail = "[email protected]"
  mailType = c.EMAIL_HOME
  'create contact
  mu.createContactEntry(name, phoneNr, phoneType, mail, mailType) 
  'thats all folks :-) 
   
  'check success, new contact should be the last entry in the list l
  Dim cs As Contacts
  Dim l As List
  l = cs.GetAll
  For i = 0 To l.Size - 1
   c = l.Get(i)
   Log(c)
  Next

End Sub

Sourcecode and binary of the lib are attached in miscUtil.zip
Copy miscUtil.jar and miscUtil.jar to your libraries folder.

b4a testproject is attached in libtest.zip

Enjoy
Michael
 

Attachments

  • libtest.zip
    124.9 KB · Views: 798
  • miscUtil.zip
    8.5 KB · Views: 846
  • newcontact.jpg
    newcontact.jpg
    7.5 KB · Views: 719
Last edited:

optimist

Member
Licensed User
Longtime User
Version 1.01 of the lib is available.
Add Contacts with multiple email-adresse and/or multiple phone-numbers

B4X:
  'Add libs phone and my miscUtil
  Dim mu As miscUtil, c As Contact
  mu.Initialize
  Dim name As String
  'define contact data
  name = "Kuno Maier"
  Dim phones As Map, mails As Map
  phones.Initialize
  phones.Put(c.PHONE_HOME, "1234567890")
  phones.Put(c.PHONE_WORK, "0987654321")
  mails.Initialize
  mails.Put(c.EMAIL_HOME, "[email protected]")
  mails.Put(c.EMAIL_WORK, "[email protected]")

  'create contact
  mu.createContactEntry2(name, phones, mails)

Suggestions welcome.
 

gobblegob

Member
Licensed User
Longtime User
Thanks this is exactly what I need.

One suggestion the name of the library implies that it does many different functions, maybe call it something more descriptive.


cheers Waz
 

mistermentality

Active Member
Licensed User
Longtime User
The zip file doesn't seem to contain a new version of the library only 1.0 , so your new example does not work :(

Dave
 

optimist

Member
Licensed User
Longtime User
The zip file doesn't seem to contain a new version of the library only 1.0 , so your new example does not work :(

Oh sorry for that, v1.04 is attached now.

But it has even improved. You can read and write the photo of the Contact.

Enjoy
 

Attachments

  • miscUtil.zip
    16.5 KB · Views: 601

optimist

Member
Licensed User
Longtime User
One suggestion the name of the library implies that it does many different functions, maybe call it something more descriptive.

This is exactly what it does for me. It provides all needed different functions for my app, not available directly in b4a. But only contact-management is interesting for common.
 
Last edited:

mistermentality

Active Member
Licensed User
Longtime User
Oh sorry for that, v1.04 is attached now.

But it has even improved. You can read and write the photo of the Contact.

Enjoy

Brilliant, will be trying this later. Very much appreciated, am trying to use this as part of a contact widget so will be great having the new abilities. Thanks :)

Dave
 

mistermentality

Active Member
Licensed User
Longtime User
Quick question as a bit curious.

I use this code:

B4X:
 phones.Initialize
  phones.Put(c.PHONE_HOME, number_entry_home.Text)
  phones.Put(c.PHONE_WORK, number_entry_work.Text)
  mails.Initialize
  mails.Put(c.EMAIL_HOME, email_entry.Text)
  mails.Put(c.EMAIL_WORK, email_entry.Text) ' save same email for both entries
  ' create the new contact
  name = name_entry.Text

  mu.createContactEntry2(name, Null, phones, mails, "", "", "", Null, Null)

where I pass Null for the values of the photo, and the home and work addresses , but when the compiled app runs I am told that the map must first be initialised.

As the only maps used are initialised in the above code does this mean instead of passing Null as a value is not allowed.

I tried creating maps but still had Null for the picture value and instead then get error about "no such element". Is there a way to pass Null for the aspects of the contact we do not have such as the photo or home address as it looks like if not all details are filled in the contact can't be saved?

Thanks.

Dave
 

optimist

Member
Licensed User
Longtime User
B4X:
  mu.createContactEntry2(name, Null, phones, mails, "", "", "", Null, Null)

where I pass Null for the values of the photo, and the home and work addresses , but when the compiled app runs I am told that the map must first be initialised.


Thank you for the hint, i will permit Null instead Maps in the next version.

For now please use initialized Maps (they accept empty strings if you dont know adresses)
Your Code could be like:

B4X:
Dim adressehome As  Map
adressehome.Initialize
adressehome.Put(0, "")  'town
adressehome.Put(1, "")  'zip
adressehome.Put(2, "")  'street
adressehome.Put(3, "")  'country
Dim adressework As  Map
adressework.Initialize
adressework.Put(0, "")  'town
adressework.Put(1, "")  'zip
adressework.Put(2, "")  'street
adressework.Put(3, "")  'country

  mu.createContactEntry2(name, Null, phones, mails, "", "", "", adressehome, adressework)
 

TheDevMan

Member
Licensed User
Longtime User
Hi optimist,

Here is something for your lib.
Delete all contacts.

B4X:
    public Boolean DeleteAllConcacts(){
       ContentResolver cr = this.m_ba.context.getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        while (cur.moveToNext()) {
            try{
                String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                cr.delete(uri, null, null);
            }
            catch(Exception e)
            {
               return false;
            }
        }

       
      return true;
       
    }
 

optimist

Member
Licensed User
Longtime User
I wonder if there is going to be an option to create a GROUP in contacts.

Get the Version 1.06 or higher and use createGroupEntry("MyNewGroup")

Example:
B4X:
Sub CreateGropTest

  Dim MyGroups As Map

  Dim j As Int
  Dim mu As miscUtil
  mu.Initialize

  'lets explore the current available groups
  MyGroups = mu.GetGroupMap
  For j=0 To MyGroups.Size - 1
    Log("Group " & MyGroups.GetKeyAt(j) & " - " & MyGroups.GetValueAt(j) )
  Next

  'create the new group
  mu.createGroupEntry("MyNewGroup")
  
  'lets explore the current available groups again
  'map shold contain the new group now
  MyGroups = mu.GetGroupMap
  For j=0 To MyGroups.Size - 1
    Log("Group " & MyGroups.GetKeyAt(j) & " - " & MyGroups.GetValueAt(j) )
  Next

End Sub
 
Last edited:

optimist

Member
Licensed User
Longtime User
Here is something for your lib.
Delete all contacts.
:sign0098:

I would never, never call this function in real live, because i collected my contacts over a log time. :)

Attached new version v1.06 contains your contribution. Thank you.
 

Attachments

  • miscUtil_1.06.zip
    7.2 KB · Views: 496

NJDude

Expert
Licensed User
Longtime User
Get the Version 1.06 or higher and use createGroupEntry("MyNewGroup")

Example:
B4X:
Sub CreateGropTest

  Dim MyGroups As Map

  Dim j As Int
  Dim mu As miscUtil
  mu.Initialize

  'lets explore the current available groups
  MyGroups = mu.GetGroupMap
  For j=0 To MyGroups.Size - 1
    Log("Group " & MyGroups.GetKeyAt(j) & " - " & MyGroups.GetValueAt(j) )
  Next

  'create the new group
  mu.createGroupEntry("MyNewGroup")
  
  'lets explore the current available groups again
  'map shold contain the new group now
  MyGroups = mu.GetGroupMap
  For j=0 To MyGroups.Size - 1
    Log("Group " & MyGroups.GetKeyAt(j) & " - " & MyGroups.GetValueAt(j) )
  Next

End Sub

FANTASTIC!!!, simple yet useful, thanks, so far is working fine.

Question, this lib is for the address book only right?, you cannot add entries to the calendar correct?


In the future, will this allow to delete a single or selected entries?, deleting the whole contacts list will be rare, but single entries might become useful.
 
Last edited:

TheDevMan

Member
Licensed User
Longtime User
:sign0098:

I would never, never call this function in real live, because i collected my contacts over a log time. :)

Attached new version v1.06 contains your contribution. Thank you.


Yeah, i know. But i needed it for one of my backup apps. Its working cool!
Its more strange when you do a backup and restore it over the others, that way you get double entries. :sign0142:
 

JOTHA

Well-Known Member
Licensed User
Longtime User
Hello optimist,

GOOD WORK! :sign0188: RESPECT!

Attached new version v1.06 contains your contribution. Thank you.
Will you add more features like ...

  • Birthday
  • First Name
  • Middle Name
  • Last Name
  • Title
  • Suffix
  • Initials
  • Web Page
  • Gender
  • Anniversary
  • Location
  • Language
  • Notes
  • Spouse
  • Children
  • Categories
  • Company
  • JobTitle
  • Hobby
  • ... etc. ... (if you wish I could send you a list)

... this would be GREAT !!!
 

Ronny

Member
Licensed User
Longtime User
Hi

I need to add several (hundred) numbers to one contact. Is that possible with miscUtil?

/Ronny
 

optimist

Member
Licensed User
Longtime User
Hi

I need to add several (hundred) numbers to one contact. Is that possible with miscUtil?

/Ronny

No, and currently not planned in the free lib. But i am ready to prioritize special features for sponsors.
 
Top