Adding new Contacts to the System-ContentProvider

News not yet published, shall be reviewed/approved by other authors.

Adding new Contacts to the System-ContentProvider

Postby plusminus » Sat May 24, 2008 12:49 pm

Adding new Contacts to the System-ContentProvider


What you learn: You will learn how to add new Contacts to the System-Contacts ContentProvider.

:idea: Designed/Tested with sdk-version: m5-rc15

Difficulty: 1.5 of 5 :)

:idea: Questions/Problems: Simply post below...

Description:
1.) Create a standard application as always
2.) Modify the [font=Courier New]AndroidManifest.xml[/font] by adding two permissions, which will allow us to read and write the system contacts:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission android:name="android.permission.READ_CONTACTS" />
  2. <uses-permission android:name="android.permission.WRITE_CONTACTS" />
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

that it finally looks similar to this:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.    package="org.anddev.contacttest">
  4.     <!-- These permission are needed, as we want to read/write to the contacts -->
  5.     <uses-permission android:name="android.permission.READ_CONTACTS" />
  6.         <uses-permission android:name="android.permission.WRITE_CONTACTS" />
  7.        
  8.     <application android:icon="@drawable/icon">
  9.         <activity android:name=".ContactTest" android:label="@string/app_name">
  10.             <intent-filter>
  11.                 <action android:name="android.intent.action.MAIN" />
  12.                 <category android:name="android.intent.category.LAUNCHER" />
  13.             </intent-filter>
  14.         </activity>
  15.     </application>
  16. </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4



3.) Copy/Use this well documented code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.contacttest;
  2.  
  3. import android.app.Activity;
  4. import android.content.ContentValues;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.provider.Contacts;
  8.  
  9. public class ContactTest extends Activity {
  10.  
  11.         /** Called when the activity is first created. */
  12.         @Override
  13.         public void onCreate(Bundle icicle) {
  14.                 super.onCreate(icicle);
  15.                 setContentView(R.layout.main);
  16.                 addSampleContact();
  17.  
  18.         }
  19.  
  20.         public void addSampleContact() {
  21.                 /* The first thing we need to do is, to create the values for contact we
  22.                  * want to insert. */
  23.                 ContentValues valuesOfNewContact = new ContentValues();
  24.                 valuesOfNewContact.put(Contacts.People.NAME, "John Doe");
  25.                 valuesOfNewContact.put(Contacts.People.COMPANY, "Funky Company");
  26.                 valuesOfNewContact.put(Contacts.People.NOTES, "Average Person");
  27.  
  28.                 /* Actually inserting the contact to the system-contacts goes like this. */
  29.                 Uri newContact = getContentResolver().insert(
  30.                                                 Contacts.People.CONTENT_URI,
  31.                                                 valuesOfNewContact);
  32.  
  33.                 /* To check whether the insertion was successful, we simply check if the
  34.                  * Uri returned is null or not. */
  35.                 if (newContact != null) {
  36.                         /* Reaching here the Uri is != null and looks similar to this one:
  37.                          * content://contacts/people/1
  38.                          */
  39.  
  40.                         /* Now lets add some more information to the contact we just created: a
  41.                          * Phone-Number. */
  42.                         ContentValues phoneValues = new ContentValues();
  43.                         phoneValues.put(Contacts.Phones.PERSON_ID,
  44.                                                         newContact.getLastPathSegment());
  45.                         phoneValues.put(Contacts.Phones.NUMBER, "(888) 555-1212");
  46.  
  47.                         /* Insert it into the contacts-phones table. */
  48.                         Uri phoneUpdate = getContentResolver().insert(
  49.                                                         Contacts.Phones.CONTENT_URI, phoneValues);
  50.  
  51.                         /* Now lets finally add a email address, by creating another
  52.                          * ContentValues-Object. */
  53.                         ContentValues emailValues = new ContentValues();
  54.                         /* Extract the ID of the new Contact. The ID is the very last path of
  55.                          * the Uri that was returned above: content://contacts/people/1 */
  56.                         String newContactID = newContact.getLastPathSegment();
  57.                         emailValues.put(
  58.                                                         Contacts.ContactMethods.PERSON_ID,
  59.                                                         newContactID);
  60.  
  61.                         emailValues.put(
  62.                                                         Contacts.ContactMethods.KIND,
  63.                                                         Contacts.ContactMethods.EMAIL_KIND);
  64.                        
  65.                         emailValues.put(
  66.                                                         Contacts.ContactMethods.TYPE,
  67.                                                         Contacts.ContactMethods.EMAIL_KIND_HOME_TYPE);
  68.                        
  69.                         emailValues.put(
  70.                                                         Contacts.ContactMethods.DATA,
  71.                                                         "john_doe_homemail@anddev.org");
  72.  
  73.                         /*
  74.                          * The difference here is that when you insert something into
  75.                          * email-table into the Contacts, be sure to use the path for the
  76.                          * person that was returned originally, and then append the path to the
  77.                          * ContactMethods "contact_methods" to the end of this Uri.
  78.                          */
  79.                         String appendix = Contacts.ContactMethods.CONTENT_URI.getPath();
  80.                         Uri emailTargetUri = newContact.buildUpon().appendPath(appendix).build();
  81.                         Uri emailUpdate = getContentResolver().insert(
  82.                                         emailTargetUri, emailValues);
  83.                 }
  84.         }
  85. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4

Thats it :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to News Review

Who is online

Users browsing this forum: No registered users and 1 guest