Android: get contact id after insert

I need to save the value of the contact ID after creating a new contact in order to be able to refer to it at another time. For example, I create a new contact, and after that I want to remove it from my contact ID, so I need to get the value of the contact ID after creating a new contact. This is how I create new contacts:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, tipoCuenta).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, cuenta).build());

//Insert some data here....

c.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

//Here, I want to retrieve contact id

How can i do this?

+4
source share
3 answers

ContentResolver.applyBatch() ContentProviderResult, . uri ( content://com.android.contacts/raw_contacts/<contact_id>).

, , uri, ..

ContentProviderResult[] results = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
int contactId = Integer.parseInt(results[0].uri.getLastPathSegment());
+7

, ,

    newContactUri = null;
    try {
        ContentProviderResult[] res = globalContext.getContentResolver()
                .applyBatch(ContactsContract.AUTHORITY, ops);
        if (res != null && res[0] != null) {
            newContactUri = res[0].uri;
            /**
             * als Ergebnis erhaelt man eine URI, mit der man die raw
             * contact-id auslesen kann.
             */
            if (debug) {
                Log.d(TAG, "URI added contact:" + res[0].uri.toString()
                        + " l: " + res.length);
            }

            subQuery(newContactUri); // setzt contactRawID

        } else if (debug)
            Log.e( ....);

    } catch (Exception e) {
        if (debug)
            Log.d( .... );
    }

subQuery

/**
 * <pre>
 * nachdem ein user angelegt ist, wird damit 
 * die contactRawID gesetzt 
 * 
 * @param contactUri
 *            ... Ergebnis aus ContentProviderResult
 * @return void
 * </pre>
 */
public void subQuery(Uri contactUri) {
    if (debug)
        Log.i(TAG, "subQuery() ");

    contactRawID = -2;
    // Content Resolver
    String contactIdString = null;
    String displayName = null;
    ContentResolver contentResolver = globalContext.getContentResolver();
    String[] mainQueryProjection = { ContactsContract.RawContacts._ID,
            ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY };

    Cursor subQueryCursor = contentResolver.query(contactUri,
            mainQueryProjection, null, null, null);
    if (subQueryCursor != null) {
        if (debug)
            Log.d(TAG, "subQueryCursor != null ");
        while (subQueryCursor.moveToNext()) {
            contactIdString = subQueryCursor.getString(0);
            displayName = subQueryCursor.getString(1);
        }
        ;
        try {
            subQueryCursor.close();
        } catch (Exception e) {
            if (debug)
                Log.d(TAG, .... );
        }
        contactRawID = Integer.parseInt(contactIdString);
    }
    return;
}

,

0

matiash:

ContentResolver.applyBatch() ContentProviderResult, . RawContact, uri RawContact ( ://com.android.contacts/raw_contacts/[raw_contact_id]).

If you are interested in raw_contact_id, then enough:

    final ContentProviderResult[] results = contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
    long rawContactId = ContentUris.parseId(results[0].uri);

But raw_contact_id on some devices may be different from contact_id. To get contact_id, you need to follow these steps:

    final ContentProviderResult[] results = contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
    final String[] projection = new String[] { ContactsContract.RawContacts.CONTACT_ID };
    final Cursor cursor = contentResolver.query(results[0].uri, projection, null, null, null);
    cursor.moveToNext();
    long contactId = cursor.getLong(0);
    cursor.close();
0
source

Source: https://habr.com/ru/post/1539602/


All Articles