Let us learn to add new contacts to the Contacts database of the device. The application will display a form prompting the user to enter information like name, address, e-mail address, and phone number of the contact. After entering the information for the new contact, when the user selects the Submit button, a new contact will be created in the Contacts database with the supplied information.
Let us create a new Android project called AddingContactsApp. To add a new contact, write the code shown in Listing 1 in the index.html file.
Listing 1 Code Written in the index.html File
Statement 1 creates a Contact object by name, newContact. Statement 2 assigns the name entered by the user to the displayName property of the newContact object. Multiple information like phone numbers, addresses, e-mail addresses, and so on, are initially kept in arrays, and thereafter the information in arrays is stored in the Contact object with the help of the ContactField object. The ContactField object is a general purpose wrapper that enables us to create a data field and then add it to a contact.
Statement 3 creates an array of one element size, phoneNums. In this array, the work phone number of the contact is kept after wrapping it in the ContactField object (statement 4). Also, the work phone number is declared as the preferred phone number of the contact. By increasing the size of the PhoneNums array, we can store the home phone number of the contact too. The phone number(s) wrapped in the ContactField object is then assigned to the phoneNumbers property of the newContact object (statement 5).
Similarly, statement 5 assigns the home e-mail address of the contact (wrapped inside the ContactField object) to the emails property of the newContact object. After defining the name, phone number, e-mail address, and address of the contact, the contact is saved in the Contacts database through statement 7. To load the index.html file, write the code as shown in Listing 2 in the Java activity file, AddingContactsAppActivity.java.
Listing 2 Code Written in the Java Activity File: AddingContactsAppActivity.java
To access, read, and write contacts in the Contacts database of the device, we need to add the following permissions in the AndroidManifest.xml file:
Our application is complete and ready to run. Upon running the application, a form prompting us to enter information of the new contact will be displayed (see Figure 1).
Information like name, address, e-mail address, and phone number of the contact has to be entered in the form, followed by clicking the Submit button. If no error occurs while saving the information of the new contact in the Contacts database, the message “Contact successfully added” will be displayed, as shown in Figure 2.
Above application currently appears as an Android application. Using PhoneGap, this application can be converted to an application that can run on different mobile platforms including iOS, Windows Phone, BlackBerry etc. To know more, and to create cross mobile platform applications, read my book, PhoneGap Build: Developing Cross Platform Mobile Applications in the Cloud that is available at