如何在Sync Adapter onPerformSync 方法中访问新添加的联系人?
How to access newly added Contact in Sync Adapter onPerformSync method?
当我将联系人添加到我的 phone 电子书时,同步适配器的 onPerformSync() 方法被触发。但我无法弄清楚如何在该方法中检索新添加的联系方式。请帮助我提供一些指导和代码片段。感谢您的帮助。
您帐户的联系人存储在 RawContacts
table. The data (e.g. phone numbers, email addresses) is stored in the Data table 中(RAW_CONTACT_ID
指向 RawContacts
中该联系人的行 _ID
table).
要检测新联系人,您的同步适配器需要维护 RawContacts
table 中的 SOURCE_ID
字段。
文档说 SOURCE_ID
(在顶部的字段列中):
String that uniquely identifies this row to its source account. Typically it is set at the time the raw contact is inserted and never changed afterwards. The one notable exception is a new raw contact: it will have an account name and type (and possibly a data set), but no source id. This indicates to the sync adapter that a new contact needs to be created server-side and its ID stored in the corresponding SOURCE_ID field on the phone.
因此您的同步适配器应该查找属于您的帐户类型且为空 SOURCE_ID
的联系人。同步联系人后,必须将 SOURCE_ID
设置为一个(非空)值来标识新联系人。
当您从服务器同步新联系人时,您需要在 Android 上插入新联系人时设置 SOURCE_ID
。如果不这样做,将导致重复(因为联系人将在下一次同步时作为新联系人同步回服务器)。
当我将联系人添加到我的 phone 电子书时,同步适配器的 onPerformSync() 方法被触发。但我无法弄清楚如何在该方法中检索新添加的联系方式。请帮助我提供一些指导和代码片段。感谢您的帮助。
您帐户的联系人存储在 RawContacts
table. The data (e.g. phone numbers, email addresses) is stored in the Data table 中(RAW_CONTACT_ID
指向 RawContacts
中该联系人的行 _ID
table).
要检测新联系人,您的同步适配器需要维护 RawContacts
table 中的 SOURCE_ID
字段。
文档说 SOURCE_ID
(在顶部的字段列中):
String that uniquely identifies this row to its source account. Typically it is set at the time the raw contact is inserted and never changed afterwards. The one notable exception is a new raw contact: it will have an account name and type (and possibly a data set), but no source id. This indicates to the sync adapter that a new contact needs to be created server-side and its ID stored in the corresponding SOURCE_ID field on the phone.
因此您的同步适配器应该查找属于您的帐户类型且为空 SOURCE_ID
的联系人。同步联系人后,必须将 SOURCE_ID
设置为一个(非空)值来标识新联系人。
当您从服务器同步新联系人时,您需要在 Android 上插入新联系人时设置 SOURCE_ID
。如果不这样做,将导致重复(因为联系人将在下一次同步时作为新联系人同步回服务器)。