Sample Code for various group operations in Android

Source: Internet
Author: User

1. Add Group

// create a groupContentValues values = new ContentValues();values.put(ContactsContract.Groups.TITLE, “GROUPNAME”);Uri uri = getContentResolver().insert(ContactsContract.Groups.CONTENT_URI, values);

Groupname is the name of the group to be created.

2. delete a group

// Delete a group // The deletion flag is not deleted. Real deletion will be automatically implemented in the same step // getcontentresolver (). delete (contactscontract. groups. content_uri, contactscontract. groups. _ ID + "=" + raw_group_id, null); // Delete URI uri = contenturis in the true sense. withappendedid (contactscontract. groups. content_uri, raw_group_id); Uri. builder B = Uri. buildupon (); B. appendqueryparameter (contactscontract. caller_is_syncadapter, "true"); uri = B. build (); getcontentresolver (). delete (Uri, null, null );

Raw_group_id is the group ID, which corresponds to the _ id field in contactscontract. Groups.

3. query all Groups

// query all groupsCursor groupCursor = getContentResolver().query(ContactsContract.Groups.CONTENT_URI, new String[]{ContactsContract.Groups.TITLE, ContactsContract.Groups._ID}, null, null, null);

Query the names and IDs of all groups.

4. Change the name of the specified group.

// update a groupContentValues values = new ContentValues();values.put(ContactsContract.Groups.TITLE, "NEWGROUPTITLE");getContentResolver().update(ContactsContract.Groups.CONTENT_URI, values, ContactsContract.Groups._ID + " = " + raw_group_id, null);

Newgrouptitle is the new group name, And raw_group_id is the group ID.

5. query all contacts in a group

// To query all contacts in a group// First, query the raw_contact_ids of all the contacts in the groupCursor groupContactCursor = getContentResolver().query(Data.CONTENT_URI, new String[]{Data.RAW_CONTACT_ID}, Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + " = " + raw_group_id, null, null);// Second, query the corresponding name of the raw_contact_idwhile(groupContactCursor.moveToNext()){Cursor contactCursor = getContentResolver().query(Data.CONTENT_URI, new String[]{Data.RAW_CONTACT_ID, StructuredName.FAMILY_NAME, StructuredName.GIVEN_NAME}, Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "' AND " + Data.RAW_CONTACT_ID + "=" + groupContactCursor.getInt(0), null, null);contactCursor.moveToNext();Log.e("TestContractGroupOperationActivity", "Member name is: " + contactCursor.getString(1) + " " + contactCursor.getString(2));contactCursor.close();}groupContactCursor.close();

6. delete a contact from the group.

// Delete a contact from a groupUri uri = Data.CONTENT_URI;Uri.Builder b = uri.buildUpon();b.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true");uri = b.build();getContentResolver().delete(uri, Data.RAW_CONTACT_ID + "=" + raw_contact_id + " AND " + ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + " =" + raw_group_id, null);

Delete the contact with ID raw_contact_id from the group with ID raw_group_id.

7. query the groups in which a contact is located.

// First, query all the group ids that a contact is inCursor groupCursor = getContentResolver().query(Data.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID}, ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "='" + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE +"' AND " + Data.RAW_CONTACT_ID + " = " + raw_contact_id, null, null);// Second, get all the corresponding group nameswhile(groupCursor.moveToNext()){Cursor groupNameCursor = getContentResolver().query(ContactsContract.Groups.CONTENT_URI, new String[]{ContactsContract.Groups.TITLE}, ContactsContract.Groups._ID + "=" + groupCursor.getInt(0), null, null);groupNameCursor.moveToNext();Log.e("Test", groupNameCursor.getString(0));groupNameCursor.close();}groupCursor.close();

Raw_contact_id indicates the contact ID.

Note that the group_raw_id contained in groupcursor may be repeated. To eliminate this duplication, you can add the content in groupcursor to a set container, then, find the group name corresponding to the element (unique group_raw_id) in the Set container.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.