// First get the id of the group you want to remove
long groupId = null;
Cursor cursor = mContext.getContentResolver.query(Groups.CONTENT_URI,
new String[] {
Groups._ID
}, Groups.TITLE + "=?", new String[] {
yourGroupTitle // Put here the name of the group you want to delete
}, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
groupId = cursor.getLong(0);
}
} finally {
cursor.close();
}
}
// Then delete your group
ArrayList<ContentProviderOperation> mOperations = new ArrayList<ContentProviderOperation>();
// Build the uri of your group with its id
Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId).buildUpon()
.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true")
.build();
ContentProviderOperation.Builder builder = ContentProviderOperation.newDelete(uri);
mOperations.add(builder.build());
// Then apply batch
try {
mContext.getContentResolver().applyBatch(ContactsContract.AUTHORITY, mOperations);
} catch (Exception e) {
Log.d("########## Exception :", ""+e.getMessage());
}
希望这对你有所帮助。
.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true")
,用于永久删除该组。 - sjngm首先查找具有特定组ID的所有联系人ID。然后为每个要删除的联系人创建一个ContentProviderOperation
,最后应用删除操作列表。
private void deletaAllInGroup(Context context, long groupId)
throws RemoteException, OperationApplicationException{
String where = String.format("%s = ?", GroupMembership.GROUP_ROW_ID);
String[] whereParmas = new String[] {Long.toString(groupId)};
String[] colSelection = new String[] {Data.CONTACT_ID};
Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI,
colSelection,
where,
whereParmas,
null);
ArrayList<ContentProviderOperation> operations =
new ArrayList<ContentProviderOperation>();
// iterate over all contacts having groupId
// and add them to the list to be deleted
while(cursor.moveToNext()){
String where = String.format("%s = ?", RawContacts.CONTACT_ID);
String[] whereParams = new String[]{Long.toString(cursor.getLong(0))};
operations.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI)
.withSelection(where, whereParams)
.build());
}
context.getContentResolver().applyBatch(
ContactsContract.AUTHORITY, operations );
}
我使用了这段代码来删除一个组。但是它的工作效果并不明显。
String groupName = "Your Group Name";
try {
ContentResolver cr = this.getContentResolver();
ContentValues groupValues = null;
groupValues = new ContentValues();
groupValues.put(ContactsContract.Groups.GROUP_VISIBLE,0);
cr.update (ContactsContract.Groups.CONTENT_URI, groupValues, ContactsContract.Groups.TITLE+ "=?", new String[]{groupName}) ;
cr.delete(ContactsContract.Groups.CONTENT_URI, ContactsContract.Groups.TITLE+ "=?", new String[]{groupValue});
}
catch(Exception e){
Log.d("########### Exception :",""+e.getMessage());
}
ContactsContract.CALLER_IS_SYNCADAPTER
并将其设置为 "true"。 - sjngm Log.e("TAG", "Click on delete");
ArrayList<ContentProviderOperation> mOperations = new ArrayList<ContentProviderOperation>();
// Build the uri of your group with its id
Uri uri = ContentUris.withAppendedId(ContactsContract.Groups.CONTENT_URI, Long.parseLong(groupModel.getGroup_id())).buildUpon()
.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true")
.build();
ContentProviderOperation.Builder builder = ContentProviderOperation.newDelete(uri);
mOperations.add(builder.build());
// Then apply batch
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, mOperations);
} catch (Exception e) {
Toast.makeText(ProspectsActivity.this, "Group is not delete.", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}