添加号码联系在Android 2.0号码、Android

2023-09-12 10:34:38 作者:我把心寄错了地址i

我试着将电话号码添加到一个的Droid手机已经存在的联系。这样做的同时,我创建一个联系人是微不足道的,作为反向引用我提供创建ContentProviderOperation时,简单地说就是0。但是,试图通过在查询的显示名称这样不起作用,找到后向引用:

Im trying to add a phone number to an already existing contact on a Droid-phone. Doing it at the same time as I create a contact is trivial, as the backreference I supply simply is 0 when creating a ContentProviderOperation. But trying to find the backreference through querying for a display name like this does not work:

Cursor rawContactsReferenceCursor = 
            contentResolver.query(Data.CONTENT_URI, 
                    new String[]{Data.RAW_CONTACT_ID},
                    Data.DISPLAY_NAME+"=\""+displayName+"\"", null, null);

虽然我得到一个原始的接触ID,下面的code只是产生一个IndexOutOfBoundException(rawConcactReferenceID是我从previous查询了变量):

While I do get a raw contact ID, the following code just generates an IndexOutOfBoundException (rawConcactReferenceID is the variable I got from the previous query):

ArrayList<ContentProviderOperation> op_list = 
    new ArrayList<ContentProviderOperation>();
op_list.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID, rawConcactReferenceID)
    .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
    .withValue(Phone.NUMBER, testNumber)
    .withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
    .withValue(Phone.LABEL, testLabel)
    .build()); 
ContentProviderResult[] result =
    contentResolver.applyBatch(ContactsContract.AUTHORITY, op_list);

最大的挑战是巨大的缺乏良好的文档。我会很满足,只是让我的手在某些工作copypasta学习。

The big challenge is a huge lack of good documentation. I would be very satisfied to just get my hands on some working copypasta to study.

干杯,

推荐答案

我找到了答案。如果你想添加几件事情的时候了,是不是原子,但嘿,谁需要愚蠢的原子?

I found an answer. It is not atomic if you want to add several things right away, but hey, who needs stupid atomicity?

ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, new Integer(contactId).intValue());
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, dataValue);
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, customLabel);
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
 
精彩推荐
图片推荐