Q:

Android - How to get saved contact id

I want to get saved contact id and i used following way to retrieve it but the problem is it's returning the wrong id.
public static String addContact(FragmentActivity activity, String displayname, String mobilenumber, String homeemail) {
    String DisplayName = displayname;
    String MobileNumber = mobilenumber;
    String homeemailID = homeemail;
    int contactID = 0;
    ArrayList<ContentProviderOperation> contentProviderOperation = new ArrayList<ContentProviderOperation>();

    contentProviderOperation.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());

    // ------------------------------------------------------ Names
    if (DisplayName != null) {
        contentProviderOperation.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.DISPLAY_NAME, DisplayName).build());
    }

    // ------------------------------------------------------ Mobile Number
    if (MobileNumber != null) {
        contentProviderOperation.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, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build());
    }

    // ------------------------------------------------------ homeEmail
    if (homeemailID != null) {
        contentProviderOperation.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Email.DATA, homeemailID)
                .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME).build());
    }

    // Asking the Contact provider to create a new contact
    try {
        // activity.getContentResolver().applyBatch(ContactsContract.AUTHORITY,
        // contentProviderOperation);
        ContentProviderResult[] res = activity.getContentResolver().applyBatch(ContactsContract.AUTHORITY, contentProviderOperation);
        Uri myContactUri = res[0].uri;
        int lastSlash = myContactUri.toString().lastIndexOf("/");
        int length = myContactUri.toString().length();
        contactID = Integer.parseInt((String) myContactUri.toString().subSequence(lastSlash + 1, length));

    } catch (Exception e) {
        e.printStackTrace();
    }
    return contactID;
}

I think it passing the wrong contact data uri.Please help me to get correct contact id.

A:

Try this way: 
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

// query time
Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);

if (cursor.moveToFirst()) {

   // Get values from contacts database:
   contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
   name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}

