Target
Helps you build an application that can read the list of contacts, add, delete contacts.
Step 1: Construction data processing layer contacts
Create a class called ContentHelper to handle contacts. This class will have the task of returning a cursor containing all contacts.
Step 2: Get a list of all contacts
To learn more about the ingredients you should learn more in the following link:
https://developer.android.com/reference/android/content/ContentResolver.html
https://developer.android.com/guide/topics/providers/content-provider-basics.html
https://developer.android.com/guide/topics/providers/contacts-provider.html
Step 3: Add contacts
Step 4: Delete contacts
This class is used to retrieve the ID of the contacts that you want to retrieve
Full source code:
https://drive.google.com/file/d/0B7V9D29Tek7yeWtNZ2N4cVM0bDA/view?usp=sharing
Helps you build an application that can read the list of contacts, add, delete contacts.
Step 1: Construction data processing layer contacts
Create a class called ContentHelper to handle contacts. This class will have the task of returning a cursor containing all contacts.
Step 2: Get a list of all contacts
Java:
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;
}
To learn more about the ingredients you should learn more in the following link:
https://developer.android.com/reference/android/content/ContentResolver.html
https://developer.android.com/guide/topics/providers/content-provider-basics.html
https://developer.android.com/guide/topics/providers/contacts-provider.html
Step 3: Add contacts
Java:
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
Java:
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();
}
}
This class is used to retrieve the ID of the contacts that you want to retrieve
Java:
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
Full source code:
https://drive.google.com/file/d/0B7V9D29Tek7yeWtNZ2N4cVM0bDA/view?usp=sharing