在安卓手机上创建 Gmail 联系人群组

4

我在我的安卓应用中尝试创建一个Gmail群组,但是未能成功创建。

我尝试的方法如下:

ArrayList<ContentProviderOperation> opsGroup = new ArrayList<ContentProviderOperation>();
            opsGroup.add(ContentProviderOperation
                    .newInsert(ContactsContract.Groups.CONTENT_URI)
                    .withValue(ContactsContract.Groups.TITLE, GroupTitle)
                    .withValue(ContactsContract.Groups.GROUP_VISIBLE, 1)
                    .withValue(ContactsContract.Groups.ACCOUNT_NAME, "sarobrindha")//my gmail name
                    .withValue(ContactsContract.Groups.ACCOUNT_TYPE,
                            "gmail.com")
                    .withValue(ContactsContract.Groups.SHOULD_SYNC, true)
                    .build());
            try {

                con.getContentResolver().applyBatch(ContactsContract.AUTHORITY,
                        opsGroup);
            } catch (Exception e) {
                e.printStackTrace();
            }

我犯了什么错误,请帮我找出来。

谢谢。


你是指“联系人群组”吗? - Maveňツ
1
是的,联系人群组就像(家人、朋友)一样。 - user4683646
尝试使用Google+/Gmail REST API :D - edwinj
1个回答

1
你可以简单地进行数据库查询来实现这一点。
看一下这个方法:
private Boolean createGrp(String GroupName) {
    // TODO Auto-generated method stub

    String s = "";
    for (int i = 0; i < InteractiveArrayAdapter.list.size(); i++) {
        if (InteractiveArrayAdapter.list.get(i).isSelected()) {
            s = s + i + " ";
        }
    }
    String s1 = null;
    s1 = editText.getText().toString();

    // Check the edittext is empty or not
    if (s1.equals("")) {
        Toast.makeText(getActivity(), "Please Enter Any Text", Toast.LENGTH_SHORT).show();
        return false;
    }

    // Check the Group is available or not
    Cursor groupCursor = null;
    String[] GROUP_PROJECTION = new String[] {
        ContactsContract.Groups._ID, ContactsContract.Groups.TITLE
    };
    groupCursor = getActivity().managedQuery(ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, ContactsContract.Groups.TITLE + "=?", new String[] {s1}, ContactsContract.Groups.TITLE + " ASC");
    Log.d("*** Here Counts: ", "** " + groupCursor.getCount());
    if (groupCursor.getCount() > 0) {
        Toast.makeText(getActivity(), "Group is already available", Toast.LENGTH_SHORT).show();
        return false;
    } else {
        //  Toast.makeText(Create_Group_Main_Page.this, "Not available", Toast.LENGTH_SHORT).show();
        //  Here we create a new Group
        try {
            ContentValues groupValues = null;
            ContentResolver cr = getActivity().getContentResolver();
            groupValues = new ContentValues();
            groupValues.put(ContactsContract.Groups.TITLE, s1);
            cr.insert(ContactsContract.Groups.CONTENT_URI, groupValues);
            Log.d("########### Group Creation Finished :", "###### Success");
        } catch (Exception e) {
            Log.d("########### Exception :", "" + e.getMessage());
            return false;
        }

    }

    groupCursor.close();
    groupCursor = null;

    Log.d(" **** Contacts add to Groups...", "**** Fine");

    String groupID = null;
    Cursor getGroupID_Cursor = null;
    getGroupID_Cursor = getActivity().managedQuery(ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, ContactsContract.Groups.TITLE + "=?", new String[] {s1}, null);
    Log.d("**** Now Empty Cursor size:", "** " + getGroupID_Cursor.getCount());
    getGroupID_Cursor.moveToFirst();
    groupID = (getGroupID_Cursor.getString(getGroupID_Cursor.getColumnIndex("_id")));
    Log.d(" **** Group ID is: ", "** " + groupID);

    getGroupID_Cursor.close();
    getGroupID_Cursor = null;


    for (int i = 0; i < InteractiveArrayAdapter.list.size(); i++) {
        if (InteractiveArrayAdapter.list.get(i).isSelected()) {
            cursor.moveToPosition(i);
            String contactID = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));

            long contact = Long.parseLong(contactID);
            long group = Long.parseLong(groupID);

            addToGroup(contact, group);

            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            Log.d(" **** Contact Added: ", "* :" + name);
        }
    }
    return true;


}

我需要在哪里输入我的电子邮件地址? - user4683646
为什么需要“电子邮件地址”? - Maveňツ
抱歉问一个愚蠢的问题,s1是什么?s = s + i + " ";为什么要这样写?我知道这段代码是为了你的需求而编写的,但即使如此,我仍然不确定如何根据我的需求进行定制。 - user4683646
@Brindha,这只是一个“列表”,用于在“群组”中添加“联系人”。 - Maveňツ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接