【问题标题】:remove contact from a specific group in android从android中的特定组中删除联系人
【发布时间】:2018-01-04 23:13:34
【问题描述】:

我正在制作一个 android 应用程序,我想从特定组中删除联系人而不是删除联系人只是从组中删除,我有组 ID 和联系人 ID,谁能告诉我执行此操作的查询, 我想实现类似 Delete contact_id=1 from group_id=2

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:

    联系人与具有 ContactsContract.CommonDataKinds.GroupMembership 记录的组相关联。您可以使用这样的方法从组中删除联系人:

    private void deleteContactFromGroup(long contactId, long groupId)
    {
        ContentResolver cr = getContentResolver();
        String where = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + "=" + groupId + " AND "
                + ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID + "=?" + " AND "
                + ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "='"
                + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'";
    
        for (Long id : getRawContactIdsForContact(contactId))
        {
            try
            {
                cr.delete(ContactsContract.Data.CONTENT_URI, where,
                        new String[] { String.valueOf(id) });
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    
    private HashSet<Long> getRawContactIdsForContact(long contactId)
    {
        HashSet<Long> ids = new HashSet<Long>();
    
        Cursor cursor = getContentResolver().query(RawContacts.CONTENT_URI,
                  new String[]{RawContacts._ID},
                  RawContacts.CONTACT_ID + "=?",
                  new String[]{String.valueOf(contactId)}, null);
    
        if (cursor != null && cursor.moveToFirst())
        {
            do
            {
                ids.add(cursor.getLong(0));
            } while (cursor.moveToNext());
            cursor.close();
        }
    
        return ids;
    }
    

    请注意,当您执行删除时,您应该指定 RAW_CONTACT_ID 而不是 CONTACT_ID。因此,您需要查询指定联系人的所有原始联系人 ID。

    您还可能需要考虑帐户数据。在这种情况下,将查询联系人 ID 更改为:

    Uri rawContactUri = RawContacts.CONTENT_URI.buildUpon()
                .appendQueryParameter(RawContacts.ACCOUNT_NAME, accountName)
                .appendQueryParameter(RawContacts.ACCOUNT_TYPE, accountType).build();
    
    Cursor cursor = getContentResolver().query(rawContactUri,
                new String[] { RawContacts._ID }, RawContacts.CONTACT_ID + "=?",
                new String[] { String.valueOf(contactId) }, null);
    

    【讨论】:

      【解决方案2】:
      public static Uri addContactToGroup(String rawContactId,String groupId)
          {
              try
              {
      
                  ContentValues values = new ContentValues();
                  values.put(Data.RAW_CONTACT_ID, rawContactId);
                  values.put(GroupMembership.GROUP_ROW_ID, groupId);
                  values.put(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE);
      
                  return getContentResolver.insert(Data.CONTENT_URI, values);
              }
              catch (Exception e)
              {}
              return Uri.EMPTY;
          }
      

      //-----------------------------------

      public static int removeContactFromGroup(String contactId,String groupId)
      {
          try
          {
              String where = Data.CONTACT_ID + " = ? AND " + Data.MIMETYPE + " = ? AND " + GroupMembership.GROUP_ROW_ID + " = ?";
              String[] args = {contactId, GroupMembership.CONTENT_ITEM_TYPE, groupId};
              return getContentResolver.delete(Data.CONTENT_URI, where, args);
          }
          catch (Exception e)
          {}
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-14
        • 2021-06-16
        • 1970-01-01
        • 1970-01-01
        • 2013-11-18
        • 1970-01-01
        • 2017-07-18
        相关资源
        最近更新 更多