在Android 2.2中插入联系人

发布于 2024-09-29 20:59:21 字数 827 浏览 1 评论 0原文

我正在尝试插入新的 RawContact 联系人,但当我通过联系人或电话簿查看联系人时,添加的 RawContact 不会显示。据我了解,如果我们创建一个 RawContact 并且没有与之关联的联系人,那么该联系人将被自动插入。我获得了 rawContactId 的有效值,并且没有抛出任何异常,因此我假设插入成功。我做错了什么或者我错过了什么吗?我正在使用开发人员网站的代码示例,只需将其粘贴到此处:

 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 am trying to insert new RawContact contacts, but the RawContact added doesn't get displayed when I view the contacts through Contacts or phonebook. As I understand if we create a RawContact and there is no contact associated with it then the contact will be automatically inserted. I get a valid value of rawContactId and no exceptions are thrown, so I assume the insertion is successful. 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); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鱼忆七猫命九 2024-10-06 20:59:21

我以为这个问题早就被遗忘了,但既然有人投票了,我假设其他人也面临着和我一样的问题。经过一番努力,我能够找出问题并插入联系人,希望这有帮助,这里是示例代码:

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

I thought this Q was long forgotten but Since someone upvoted it, I am assuming someone else also faces the same problem as me. 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);
挽清梦 2024-10-06 20:59:21

一位客户向我报告,上述答案(由 Als 提供)中的解决方案在某些 HTC 设备上不起作用。经过几天的沮丧,我想出了这个解决方案:

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

A client reported to me that the solution in the answer above(by Als) doesn't work on some HTC devices. 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;
}
林空鹿饮溪 2024-10-06 20:59:21

要创建可见联系人,它需要属于可见组。查看计算机上的 Gmail 联系人以查看组和可见性。

要在设备上找到可见的组,请执行以下操作:

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

要将组添加到 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);

或 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:

我不明白您答案中的第三段,其中写着:

values.put(Data .MIMETYPE、数据.CONTENT_TYPE)
...

没有这样的 MIMETYPE。 (我在运行 HTC Android 2.2 的 HTC Desire 上检查过)。

谢谢。

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.

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:

        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.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:

I don't understand the 3rd paragraph in your answer where it says:

values.put(Data.MIMETYPE, Data.CONTENT_TYPE)
...

There is no such MIMETYPE. (I checked it on HTC Desire running HTC Android 2.2).

Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文