[英]Inserting contacts in Android 2.2
I am trying to insert new RawContact
contacts, but the RawContact
added doesn't get displayed when I view the contacts through Contacts or phonebook. 我正在尝试插入新的
RawContact
联系人,但是当我通过“联系人”或电话簿查看联系人时,添加的RawContact
不会显示。 As I understand if we create a RawContact
and there is no contact associated with it then the contact will be automatically inserted. 据我所知,如果我们创建一个
RawContact
并且没有与之关联的联系人,那么将自动插入联系人。 I get a valid value of rawContactId
and no exceptions are thrown, so I assume the insertion is successful. 我得到
rawContactId
的有效值,并且没有抛出异常,所以我假设插入成功。 Am I doing anything wrong or am I missing something? 我做错了什么或者我错过了什么? I am using the code example from developer site, just pasting it here:
我正在使用开发人员网站的代码示例,只需将其粘贴到此处:
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, accountType);
values.put(RawContacts.ACCOUNT_NAME, accountName);
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, "Mike Sullivan");
getContentResolver().insert(Data.CONTENT_URI, values);
I thought this Q was long forgotten but Since someone upvoted it, I am assuming someone else also faces the same problem as me. 我以为这个Q早就被遗忘了,但是由于有人投了它,我假设其他人也面临着和我一样的问题。 After a little struggle I was able to figure out the problem and insert contacts, Hope this helps, here is the sample code:
经过一番努力,我能够找出问题并插入联系人,希望这有帮助,这里是示例代码:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME,null )
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, "9X-XXXXXXXXX")
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
.build());
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
A client reported to me that the solution in the answer above(by Als) doesn't work on some HTC devices. 客户向我报告,上述答案中的解决方案(由Als提供)对某些HTC设备不起作用。 So after a few days of frustration I came up with this solution:
所以经过几天的挫折之后,我想出了这个解决方案:
String name = "First Family";
String phone = "0123456789";
ContentValues values = new ContentValues();
values.put(Data.DISPLAY_NAME, name);
Uri rawContactUri = c.getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
long contactId = Util.getContactId(c, rawContactId);
System.out.println("rawContactId = " + rawContactId);
System.out.println("contactId = " + contactId);
values.clear();
values.put(Phone.NUMBER, phone);
values.put(Phone.TYPE, Phone.TYPE_OTHER);
values.put(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Data.RAW_CONTACT_ID, rawContactId);
c.getContentResolver().insert(Data.CONTENT_URI, values);
values.clear();
values.put(Data.MIMETYPE, Data.CONTENT_TYPE);
values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
values.put(Data.RAW_CONTACT_ID, rawContactId);
c.getContentResolver().insert(Data.CONTENT_URI, values);
values.clear();
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
values.put(Data.RAW_CONTACT_ID, rawContactId);
c.getContentResolver().insert(Data.CONTENT_URI, values);
public static long getContactId(Context context, long rawContactId) {
Cursor cur = null;
try {
cur = context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, new String[] { ContactsContract.RawContacts.CONTACT_ID }, ContactsContract.RawContacts._ID + "=" + rawContactId, null, null);
if (cur.moveToFirst()) {
return cur.getLong(cur.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cur != null) {
cur.close();
}
}
return -1l;
}
To have a visible contact created, it needs to belong to a visible group. 要创建可见联系人,它需要属于可见组。 Have a look at gmail contacts on your computer to view groups and visibility.
查看计算机上的gmail联系人以查看组和可见性。
To find a visible group on the device, do something like this: 要在设备上查找可见组,请执行以下操作:
Long myContactsGroupId = null;
sqlWhere = ContactsContract.Groups.ACCOUNT_TYPE + " = 'com.google' AND " + ContactsContract.Groups.GROUP_VISIBLE + " = 1";
Cursor cursor = getContentResolver().query(ContactsContract.Groups.CONTENT_URI, new String[] {"_id"}, sqlWhere, null, "_id");
if (cursor.moveToFirst()) {
myContactsGroupId = cursor.getLong(0);
}
To add the group to the rawContact: 要将组添加到rawContact:
cv.clear();
cv.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);
cv.put(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, myContactsGroupId);
cv.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
getContentResolver().insert(ContactsContract.Data.CONTENT_URI, cv);
Or the ops version: 或ops版本:
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, myContactsGroupId)
.build());
@anqe1ki11er: @ anqe1ki11er:
I don't understand the 3rd paragraph in your answer where it says: 我不明白你答案中的第3段,它说:
values.put(Data.MIMETYPE, Data.CONTENT_TYPE) ... values.put(Data.MIMETYPE,Data.CONTENT_TYPE)......
There is no such MIMETYPE. 没有这样的MIMETYPE。 (I checked it on HTC Desire running HTC Android 2.2).
(我在运行HTC Android 2.2的HTC Desire上查了一下)。
Thanks. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.