add contact to a group in Android

前端 未结 4 955
南旧
南旧 2021-01-16 22:15

I am working on android apps. I want to add a contact in android phone group. The code I am using is below:

    ContentValues values = new ContentValues();
          


        
4条回答
  •  猫巷女王i
    2021-01-16 22:47

    Bellow code is worked perfect in my side. So, Please try it or you can download sample example from chetanbhalala

    try
    {
    
       // Add selected contact to selected group
       ContentValues values = new ContentValues();
           values.put(ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID,Integer.parseInt(245)); // 245 is a contact id, replace with selected contact id
    
           values.put(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID,3);// 3 is a group id, replace with selected group id
    
           values.put(ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE,ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);
    
           ContextWrapper context = this;
           context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
           // End add contact to group code
    }
    catch (Exception e) 
    {
       // TODO: handle exception
       Log.d("add group error :", ""+ e.getMessage().toString());
    }
    

提交回复
热议问题