THIS CONTENT DOWNLOAD SHORTLY

Objective

Main objective of this tutorial is to help you to getting, adding and deleting the contacts from android device.

 

 

Step 1 Create ContactHelper class.

First we create ContactHelper class into our project. In this class we create methods for Getting , Adding and Deleting contacts.

 

Step 2 Read Contacts

Add this method in to ContactHelper class for getting all the Contacts. This method returns the cursor of all the contacts.

public static Cursor getContactCursor(ContentResolver contactHelper, String startsWith) {

    String[] projection = { ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER };
    Cursor cur = null;
    
    try {
        if (startsWith != null && !startsWith.equals("")) {
            cur = contactHelper.query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " like \"" + startsWith + "%\"", null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        } else {
            cur = contactHelper.query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        }
        cur.moveToFirst();
    } 
    catch (Exception e) {
        e.printStackTrace();
    }
    return cur;
}

For more information about ContentResolver class please refer following link:

 

Step 3 Insert Contacts

Add this method in to ContactHelper.java class for adding the Contacts. This method returns the boolean value with contact addition done.

public static boolean insertContact(ContentResolver contactAdder, String firstName, String mobileNumber) {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());
    
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,firstName).build());
    
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,mobileNumber).withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_MOBILE).build());

    try {
        contactAdder.applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (Exception e) {
    return false;
    }
return true;
}
 

Step 4 Delete Contacts

Add this method in to ContactHelper class for deleting the Contacts. In this method we need Contact ID so, we use one another method for getting Contact ID.

public static void deleteContact(ContentResolver contactHelper, String number) {

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    String[] args = new String[] { String.valueOf(getContactID(contactHelper, number)) };

    ops.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI).withSelection(RawContacts.CONTACT_ID + "=?", args).build());

    try {
        contactHelper.applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {
        e.printStackTrace();
    } catch (OperationApplicationException e) {
        e.printStackTrace();
    }
}
private static long getContactID(ContentResolver contactHelper,String number) {
    Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    String[] projection = { PhoneLookup._ID };
    Cursor cursor = null;
    
    try {
        cursor = contactHelper.query(contactUri, projection, null, null,null);

        if (cursor.moveToFirst()) {
            int personID = cursor.getColumnIndex(PhoneLookup._ID);
            return cursor.getLong(personID);
        }
        return -1;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
    }
return -1;
}

Call above three method into your Main method for getting, adding and deleting the contacts. For this, Download the Example which is attached with this tutorial.

To run this example on MarshMallow, you have to check permissions at runtime.

I hope you enjoy this tutorial and it would be helpful to you. Got an Idea of Android App Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Android App Development Company in India.

An entrepreneur who has founded 2 flourishing software firms in 7 years, Tejas is keen to understand everything about gaming - from the business dynamics to awesome designs to gamer psychology. As the founder-CEO of a company that has released some very successful games, he knows a thing or two about gaming. He shares his knowledge through blogs and talks that he gets invited to.