long groupId = Constant.createGroupId; ContentValues values = new ContentValues(); for(int i=0,len=Constant.contactList.size();i<len;i++) { values.clear(); values.put(Data.RAW_CONTACT_ID, queryForRawContactId(getContentResolver(), Constant.contactList.get(i).getContactId())); values.put(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE); values.put(GroupMembership.GROUP_ROW_ID, groupId); getContentResolver().insert(Data.CONTENT_URI, values); } } public static long queryForRawContactId(ContentResolver cr, long contactId) { Cursor rawContactIdCursor = null; long rawContactId = -1; try { rawContactIdCursor = cr.query(RawContacts.CONTENT_URI, new String[] { RawContacts._ID }, RawContacts.CONTACT_ID + "=" + contactId, null, null); if (rawContactIdCursor != null && rawContactIdCursor.moveToFirst()) { // Just return the first one. rawContactId = rawContactIdCursor.getLong(0); } } finally { if (rawContactIdCursor != null) { rawContactIdCursor.close(); } } return rawContactId; }
Android-通讯录:向分组Group添加联系人
时间: 2024-10-09 23:01:00