3

如何以编程方式删除特定的 android 联系人组?

我试过这个,

在 Android 上使用联系人组删除时出现问题

对我不起作用。请告诉我任何想法或建议。这对我真的很有帮助。

提前谢谢!!!

4

4 回答 4

4

我找到了一种正确删除组的方法。您需要使用适当的查询获取要删除的组的 id,然后您可以使用此 id 和 Groups.CONTENT_URI 删除该组。

我在下面发布了一个示例(只需将其调整为您的代码)。

// 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());
}

希望它会有所帮助。

于 2013-04-15T09:32:02.650 回答
1

首先找到所有具有特定组 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 );
}
于 2012-12-24T11:53:09.203 回答
0

我使用此代码删除了一个组。但工作不是很清楚。

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()); 
        }

运行此代码后。组被删除。我去电话联系人或人和搜索组。它没有显示。但是,如果我在程序中以编程方式读取所有组,则会显示已删除的组。

于 2013-02-02T05:45:03.637 回答
0

试试这个代码删除组 public void checkAndDeleteGroup(final GroupModel groupModel){

    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();
    }

}
于 2017-05-19T11:40:12.350 回答