4

在我的 android 应用程序中,我尝试创建一个 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();
            }

我犯了什么错误。请帮我弄清楚

谢谢

4

1 回答 1

1

您可以简单地进行数据库查询来这样做。

看看这个method

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;


}
于 2015-03-23T13:24:20.440 回答