Alin
Alin

Reputation: 14571

Query, backup, delete, insert Contacts in Android

This question should be a starting point to all of us who want to manipulate contacts in Android.

First things first

As I am aware, since API level 5 the Contacts API has changed, so in order to make the application work correct I need to check what android os is on the phone and if prior 5 use one content provider or else use the newer one. The only annoyance in this case is the warnings of deprecated I get. The application is build against Android 2.3.3 but needs to work from 1.5+

1. Querying contacts

This is the easiest part to do. Usually querying means getting data like Contact name, phones, picture, email and displaying it on a listview. For instance here is how I've done it in API prior 5

            String[] projectionPeople = new String[] {People._ID, People.NAME,};
            String[] projectionPhone = new String[] {Phones.NUMBER};

                try {
                    // Get the base URI for People table in Contacts content provider. 
                    // which is: content://contacts/people/ 
                    Uri contactUri = People.CONTENT_URI; 
                    ContentResolver resolver = getContentResolver();

                    Cursor phonesCursor = null;
                    Cursor peopleCursor = resolver.query (contactUri, 
                                                        projectionPeople, //Which columns to return. 
                                                        "People.NAME is not null", // WHERE clause--we won't specify. 
                                                        null, // Selection Args?? 
                                                        People.DEFAULT_SORT_ORDER); // Order-by name 

                    if (peopleCursor != null && peopleCursor.getCount() >0)
                    {
                        // go to the beginning of the list 
                        peopleCursor.moveToFirst(); 

                        do
                        {
                                    //do something with current contact info
                            phoneUri= Uri.withAppendedPath(personUri, Contacts.People.Phones.CONTENT_DIRECTORY); 

                            phonesCursor = resolver.query(phoneUri, 
                                                         projectionPhone, 
                                                         null, 
                                                         null, 
                                                         Phones.DEFAULT_SORT_ORDER); 

                            if (phonesCursor!=null && phonesCursor.getCount()>0)
                            {
                                phonesCursor.moveToFirst();
                                lstPhones = new ArrayList<String>();

                                do
                                {
                                    //add phone numbers to a List<String> for instance
                                } while (phonesCursor.moveToNext());


                            if (phonesCursor != null && !phonesCursor.isClosed())
                                phonesCursor.close();

                        }  while (peopleCursor.moveToNext());

                        if (peopleCursor != null && !peopleCursor.isClosed())
                            peopleCursor.close();
                    }
                }
                catch (Exception ex) 
                {

                }
            }

Haven't tried it yet on the new api but the cursor should be like

final String[] projection = new String[] {
                                          RawContacts.CONTACT_ID,   // the contact id column
                                          RawContacts.DELETED       // column if this contact is deleted
};

final Cursor rawContacts = managedQuery(RawContacts.CONTENT_URI,    // the URI for raw                                                                                                              contact provider
                                            projection
                                             null,                  // selection = null, retrieve all entries
                                             null,                  // selection is without parameters
                                             null);                 // do not order

Sure, this needs to be elaborated a bit more, but it should provide the basics of simple query against Contacts content provider

2. Backup

My first thought on this was: if I know the Id of a Contact, I create tables in a sqlite database exactly how the cursor columns are and insert all the data into my tables. This is not an easy task as it requires a lot of codding not to mention that different apis have different table structures. What would be the best solution to backup one contact or multiple contacts ?

3. Delete

This should work on all apis using content providers, but data is spread on many packages and uris and I'm not sure from where to delete

4. Insert

After a contact is backed up, I may need to restore/insert it again. As in case of deletion, on which uris do I need to insert ?

Please, let's try to elaborate this issues so in the futures, who needs to use Contacts in Android apps could take this question as a solid starting point. Thank you stackoverflow community.

Upvotes: 4

Views: 3392

Answers (1)

Alin
Alin

Reputation: 14571

Here is a good starting point http://developer.android.com/resources/samples/BusinessCard/index.html

Upvotes: 1

Related Questions