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

时间:2012-12-24 11:34:57

标签: android

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

我试过了,

Issue using Contact Group delete on Android

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

提前谢谢!!!

4 个答案:

答案 0 :(得分: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());
}

希望它会有所帮助。

答案 1 :(得分: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 );
}

答案 2 :(得分: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()); 
        }

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

答案 3 :(得分:0)

尝试使用此代码删除组  public void checkAndDeleteGroup(final GroupModel groupModel){

main.cpp(15): error C2971: 'params::convert_traits': template parameter 'ParamName': 'params::CLIENT_LOGIN': a variable with non-static storage duration cannot be used as a non-type argument
main.cpp(10): note: see declaration of 'params::convert_traits'
main.cpp(6): note: see declaration of 'params::CLIENT_LOGIN'
main.cpp(26): error C2971: 'params::convert_traits': template parameter 'ParamName': 'params::CLIENT_LOGIN': a variable with non-static storage duration cannot be used as a non-type argument
main.cpp(10): note: see declaration of 'params::convert_traits'
main.cpp(6): note: see declaration of 'params::CLIENT_LOGIN'