Skip to content Skip to sidebar Skip to footer

How To Get Contact Id After Add A New Contact In Android?

I use following code for new contacts added to phone. private static void addContact(Account account, String name, String username,String phone,String email) { Log.i(TAG, 'Add

Solution 1:

The bulk insert that you do with the operation list will return a list of results (URIs I think, but it's been a few months since I did this), I believe you can get the ID you want from the first of these results

 long rawContactId = ContentUris.parseId(rawContactUri);

Solution 2:

After Applying the badge and receive the last segment you receive: ContactsContract.RawContacts._ID If you want to get Contact_id you should make this query:

(rowId - is the last segment you received before)

String[] projections = new String[]{ContactsContract.RawContacts.CONTACT_ID};
String select = ContactsContract.RawContacts._ID + "= ?";
String[] selectionArgs = new String[]{rowId};
Cursor cursor = context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projections, select, selectionArgs, null);
    String contactId = null;
    if (cursor.getCount() == 0) {
          Log.d("aaa","Couldn't find row: " + rowId);
          return null;
    }

    if (cursor.moveToNext()) {
         int columnIndex;
         columnIndex = cursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID);
         if (columnIndex == -1) {
               Log.d("aaa","Couldn't get contact id");
         } else {
               contactId = cursor.getString(columnIndex);
         }
    }

    cursor.close();
    return contactId;

Post a Comment for "How To Get Contact Id After Add A New Contact In Android?"