add contact to a group in Android

前端 未结 4 928
南旧
南旧 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条回答
  • 2021-01-16 22:42

    there is a issue in chetanbhalala / Add-contact-to-group-in-android with selected item on click function.

    i solved it like this way

        @Override
    public void onClick(View v) {
        if(v == btn_add)
        {
            if(selected_items_id.size() > 0)
            {
                selected_items_id.clear();
            }
            ListView parent = getListView();
            SparseBooleanArray choices = parent.getCheckedItemPositions();
            for (int i = 0; i < parent.getCount(); i++)
            {                
                if (choices.get(i))
                {
                    selected_items_id.add(""+ group_contacts_id.get(i));
                      System.out.println(group_contacts_id.get(i)+"group_contacts_id is");
                }  
            } 
    
            if(selected_items_id.size() > 0)
            {
                add_group();
            }
            else
            {
                Toast.makeText(this, "please select contact:", Toast.LENGTH_LONG).show();
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-16 22:45
        Intent intent = new Intent(Intent.ACTION_INSERT);
                    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    
                    intent.putExtra(ContactsContract.Intents.Insert.NAME, fullname);
    intent.putExtra(ContactsContract.Intents.Insert.PHONE,phoneno);
                    intent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
                    intent.putExtra(ContactsContract.Intents.Insert.NOTES,
                            "Imported from moodle");
                    intent.putExtra(ContactsContract.Intents.Insert.POSTAL, city
                            + " " + country);
    
                    startActivityForResult(intent, 1);
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                switch (requestCode) {
                case 1:
                    if (requestCode == 1) {
    
                    }
                    super.onActivityResult(requestCode, resultCode, data);
                }
            }
        }
    
    0 讨论(0)
  • 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());
    }
    
    0 讨论(0)
  • 2021-01-16 22:49

    1)

    public static void createNewGroup(String name)
        {
            try
        {
            ContentValues groupValues = new ContentValues();
            groupValues.put(Groups.TITLE, name);
            groupValues.put(Groups.SHOULD_SYNC, true);
            groupValues.put(Groups.GROUP_VISIBLE, 1);
            groupValues.putNull(Groups.ACCOUNT_TYPE);
            groupValues.putNull(Groups.ACCOUNT_NAME);
            getContentResolver().insert(Groups.CONTENT_URI, groupValues);
            return true;
        }
        catch (Exception e){}
        }
    

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

    3)

    public static Cursor getContactGroupId(String contactId)
        {
            Uri uri = ContactsContract.Data.CONTENT_URI;
            String[] columns = new String[] { GroupMembership.GROUP_ROW_ID };
            String where = Data.CONTACT_ID + " = ? AND " + Data.MIMETYPE + " = ?";
            String[] args = new String[] {contactId, GroupMembership.CONTENT_ITEM_TYPE };
            return Cursor contacts = getContentResolver().query(uri, columns, where, arg, null);
        }
    

    4)

    public static Cursor getGroupsList(@Nullable String[] project,@Nullable String where,@Nullable String[] args,@Nullable String sort)
        {
    
            return getContentResolver().query(Groups.CONTENT_URI, project, where, args, sort);
        }
    
    0 讨论(0)
提交回复
热议问题