我正在开发Android应用程序。我想在android手机组中添加一个联系人。我正在使用的代码如下:
ContentValues values = new ContentValues();
values.put(ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID,personId);
values.put(
ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID,GROUP_ID);
values.put(
ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE,ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);
Log.d("values :", ""+ values);
this.getContentResolver().insert(
ContactsContract.Data.CONTENT_URI, values);
不幸的是,这不起作用。有没有人看到上述代码有任何明显错误?
答案 0 :(得分:1)
chetanbhalala / Add-contact-to-group-in-android中存在点击功能选定项目的问题。
我这样解决了
@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();
}
}
}
答案 1 :(得分:0)
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);
}
}
}
答案 2 :(得分:0)
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);
}
答案 3 :(得分:-1)
Bellow代码在我身边很完美。所以,请尝试一下,或者您可以从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());
}