[英]I want to add information into the contact according to the ID number, but it always being added to the wrong name.
I want to add a telephone number into the contact according to the ID number, but it always being added to the wrong name. 我想根据ID号将电话号码添加到联系人中,但始终将其添加到错误的名称中。 Can anyone tell me the reason why this happened. 谁能告诉我发生这种情况的原因。 It runs well in virtual device and sony mobile phone , but it come to an error as I said above in a new Samsung moble phone. 它可以在虚拟设备和索尼手机上很好地运行,但是如我上面在新的三星移动电话中所说,它出现了错误。 I can clearly confirm that the ID number is right These is the source code: 我可以清楚地确认ID号正确无误这些是源代码:
ContentValues values = new ContentValues ();
values.clear();
values.put(ContactsContract.Data.RAW_CONTACT_ID, contactID);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, shortNumber);
values.put(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_OTHER);
getContentResolver().insert(Data.CONTENT_URI,values);
values.clear();
The answer here may help: Inserting contacts in Android 2.2 . 此处的答案可能会有所帮助: 在Android 2.2中插入联系人 。
It seems the code above does not work on all devices as per the comments there. 根据上面的注释,似乎上面的代码不适用于所有设备。 The answer posted by Alok Save https://stackoverflow.com/users/452307/alok-save , suggests using the applyBatch() method as follows: Alok Save发布的答案https://stackoverflow.com/users/452307/alok-save ,建议如下使用applyBatch()方法:
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(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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.