如何在Android程序中以编程方式删除联系人组?

3

我们想要删除所有联系人... - NagarjunaReddy
不,我不想删除联系人。我只想删除分组。我已经有一个程序可以从分组中移除联系人。谢谢支持。 - Thirumalvalavan
Android联系人分组。例如:同事、家人、收藏夹。请查看此链接,它会对您有所帮助。http://stackoverflow.com/questions/13026025/get-contacts-from-contact-group-based-on-group-id - Thirumalvalavan
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());
}

希望这对你有所帮助。


最重要的一行是.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true"),用于永久删除该组。 - sjngm

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

我尝试了这段代码。在模拟器中,只有组中的联系人被删除,但是组没有被删除。在手机上,联系人和组都没有被删除。(我尝试了HTC Wildfire S) - Thirumalvalavan
您也想删除组名吗? - Sahil Mahajan Mj

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

运行此代码后,该组已被删除。我去电话联系人或通讯录中搜索该组,但未显示。但是,如果我在程序中以编程方式读取所有组,则已删除的组将显示出来。

您必须使用查询参数 ContactsContract.CALLER_IS_SYNCADAPTER 并将其设置为 "true"。 - sjngm

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

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接