我正在将我的应用程序与Android默认联系人应用程序集成.我想在每个联系人详细信息中显示选项“xyz using MyApp”.我能够在Accounts Section中看到我的应用程序,其中包含同步联系人的选项,但我的应用程序仍未合并使用现有联系人,而是创建新联系人并合并其中.
performSync()方法
private static voID addContact(ContentResolver contentResolver,int name, int phoneNumber) { Log.i("XYZ", "Adding contact: " + name); ArrayList<ContentProvIDerOperation> operationList = new ArrayList<ContentProvIDerOperation>(); //Create our RawContact ContentProvIDerOperation.Builder builder = ContentProvIDerOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI); builder.withValue(ContactsContract.RawContacts.ACCOUNT_name, name); builder.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.example.xyz.myapplication"); builder.withValue(ContactsContract.RawContacts.SYNC1, phoneNumber); operationList.add(builder.build()); //Create a Data record of common type 'Structuredname' for our RawContact builder = ContentProvIDerOperation.newInsert(ContactsContract.Data.CONTENT_URI); builder.withValueBackReference(ContactsContract.CommonDataKinds.Structuredname.RAW_CONTACT_ID, 0); builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Structuredname.CONTENT_ITEM_TYPE); builder.withValue(ContactsContract.CommonDataKinds.Structuredname.disPLAY_name, name); operationList.add(builder.build()); //Create a Data record of custom type "vnd.androID.cursor.item/vnd.com.example.xyz.myapplication.profile" to display a link to the Last.fm profile builder = ContentProvIDerOperation.newInsert(ContactsContract.Data.CONTENT_URI); builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0); builder.withValue(ContactsContract.Data.MIMETYPE, "vnd.androID.cursor.item/vnd.com.example.xyz.myapplication.profile"); builder.withValue(ContactsContract.Data.DATA1, phoneNumber); builder.withValue(ContactsContract.Data.DATA2, "Last.fm Profile"); builder.withValue(ContactsContract.Data.DATA3, "VIEw profile"); operationList.add(builder.build()); try { contentResolver.applyBatch(ContactsContract.AUTHORITY, operationList); } catch (Exception e) { Log.e("XYZ", "Something went wrong during creation! " + e); e.printstacktrace(); }}
解决方法:
在您的addContact代码中,您缺少告诉Contacts DB将新的原始联系人加入现有联系人的部分,以便该联系人现在将包含您的原始联系人,并且在打开该联系人时将显示您的特定于应用程序的行.联系人应用.
查看有关如何将RawContact加入现有联系人的答案:why won’t contacts aggregate?
您可能需要将一些RawContact ID传递给addContact方法,这样它才能将两者连接在一起.
UPDATE
我们不是将聚合 *** 作与RawContact插入 *** 作一起应用,而是尝试将两个applyBatch调用分开,让我们将所有现有原始联系人的新原始联系人聚合,而不仅仅是其中一个.
请尝试以下代码,确保将现有的contact-ID(非raw-contact ID)和新的raw-contact-ID传递给它.
private voID joinIntoExistingContact(long existingContactID, long newRawContactID) { // get all existing raw-contact-IDs that belong to the contact-ID List<Long> existingRawIDs = new ArrayList<>(); Cursor cur = getContentResolver().query(RawContacts.CONTENT_URI, new String[] { RawContacts._ID }, RawContacts.CONTACT_ID + "=" + existingContactID, null, null); while (cur.movetoNext()) { existingRawIDs.add(cur.getLong(0)); } cur.close(); Log.i("Join", "Found " + existingRawIDs.size() + " raw-contact-IDs"); List<ContentProvIDerOperation> ops = new ArrayList<ContentProvIDerOperation>(); // go over all existing raw IDs, and join with our new one for (Long existingRawID : existingRawIDs) { Builder builder = ContentProvIDerOperation.newUpdate(AggregationExceptions.CONTENT_URI); builder.withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER); builder.withValue(AggregationExceptions.RAW_CONTACT_ID1, newRawContactID); builder.withValue(AggregationExceptions.RAW_CONTACT_ID2, existingRawID); ops.add(builder.build()); } contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);}
附:
不要开两个duplicate questions,一个就够了.
另一个更新
你似乎对ID有些困惑.
有数据ID,RawContact ID和联系人ID.
CommonDataKinds.Phone._ID将返回一个数据ID,用于标识存储该电话号码的数据表中的特定行.
您也可以从Phone表中获取其他ID,使用:
CommonDataKinds.Phone.RAW_CONTACT_ID
CommonDataKinds.Phone.CONTACT_ID
你可以在这里阅读更多:
https://stackoverflow.com/a/50084029/819355
以上是内存溢出为你收集整理的android – 新联系人创建而不是更新现有联系人全部内容,希望文章能够帮你解决android – 新联系人创建而不是更新现有联系人所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)