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

问题描述:

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

我想这一点,

Issue using Contact Group delete on Android

没有工作给我。请告诉我任何想法或建议。这对我真的很有帮助。

先谢谢了!

+0

我们要删除所有联系人... – NagarjunaReddy

+0

不,我不希望删除的联系人。我只想删除组。我已经有一个程序从组中删除联系人。并感谢支持 – Thirumalvalavan

+0

Android联系人组。喜欢同事,家庭,收藏夹。请看这个链接。它会帮助你。 http://*.com/questions/13026025/get-contacts-from-contact-group-based-on-group-id – Thirumalvalavan

首先找到具有特定组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); 
} 
+0

感谢您的支持。我会尝试这个代码。 – Thirumalvalavan

+0

我试过这段代码。在模拟器中,联系人仅在组中删除,但组未删除。 在手机中,联系人和组都不会被删除。 (我试过htc wildfire s) – Thirumalvalavan

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

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

+0

必须将“ContactsContract.CALLER_IS_SYNCADAPTER”查询参数应用于“true”。 – sjngm

我找到一个方法来正确地删除组。 你需要让你想用适当的查询来删除该组的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()); 
} 

希望它会有所帮助。

+0

最重要的行是'.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER,“true”)'来永久删除组。 – sjngm

尝试进行删除组 公共无效checkAndDeleteGroup代码(最终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(); 
    } 

}