【问题标题】:Want to create a new group in contacts programmatically想以编程方式在联系人中创建一个新组
【发布时间】:2011-09-02 07:58:01
【问题描述】:

我想创建一个新的联系人组。我可以查询组并显示所有组名但我无法在 android 中创建组我尝试创建联系人方法但未创建...

ContentResolver cr = this.getContentResolver();
    groupValues = new ContentValues();
    Log.e("Group","start");
    groupValues.put(android.provider.Contacts.GroupMembership.GROUP_ID, 4);
    groupValues.put(android.provider.Contacts.GroupMembership.NAME, "Sriseshaa");
    groupValues.put(android.provider.Contacts.GroupMembership.PERSON_ID, 1);

    cr.insert(android.provider.Contacts.GroupMembership.CONTENT_URI, groupValues);

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:

    我找到了答案。我通过两种方式找到了答案,但我不知道哪个是正确的或最好的使用方式。我在这里分享这些..

    添加联系人的简单方法,

    ContentValues groupValues;
    create group()
    {
     ContentResolver cr = this.getContentResolver();
     groupValues = new ContentValues();
     groupValues.put(ContactsContract.Groups.TITLE, "MyContactGroup");
     cr.insert(ContactsContract.Groups.CONTENT_URI, groupValues);
    }
    

    使用 ContentProviderOperation 的另一种方法

     private void createGroup() {
        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    
        ops.add(ContentProviderOperation
                .newInsert(ContactsContract.Groups.CONTENT_URI)
                .withValue(ContactsContract.Groups.TITLE, "SRI").build());
        try {
    
            getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    
        } catch (Exception e) {
            Log.e("Error", e.toString());
        }
    
    }
    

    谢谢

    【讨论】:

    • 默认情况下,组 1.Friends 2. family 3. CoWorkes 中已经存在 3 个选项。现在我的问题是如何在这些选项中将联系人添加到特定组。
    【解决方案2】:

    你为什么用groupValues.put(android.provider.Contacts.GroupMembership.GROUP_ID, 4);指定组ID,它的androids工作来确定组ID,你不能指定它,因为你不知道这个id是否已经被占用了。

    【讨论】:

      【解决方案3】:

      adithi 的回答对于 Android 4.2.2 来说已经足够了,其中 Contacts manager 应用程序的名称是 "Contacts" ,但是由该代码创建的组不会在 Android 4.4,6 上显示,其中 Contacts manager 应用程序的名称是“人们”。

      在插入时添加帐户类型/名称信息后会显示该组。

      private void createGroup() {
      
          ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
      
              ops.add(ContentProviderOperation
                      .newInsert(ContactsContract.Groups.CONTENT_URI)
                      .withValue(
                              ContactsContract.Groups.TITLE,
                              Constants.CC_CONTACT_GROUP_TITLE)
                      .withValue(
                              ContactsContract.Groups.ACCOUNT_TYPE,
                              Constants.CC_CONTACT_GROUP_ACCOUNT_TYPE)
                      .withValue(
                              ContactsContract.Groups.ACCOUNT_NAME,
                              Constants.CC_CONTACT_GROUP_ACCOUNT_NAME)
                      .build());
          try {
      
              getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
      
          } catch (Exception e) {
              Log.e("Error", e.toString());
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-01
        • 2012-01-14
        • 2015-03-15
        • 2017-02-23
        • 1970-01-01
        • 1970-01-01
        • 2016-12-26
        相关资源
        最近更新 更多