将隐藏的同步标识符存储在 android 个原始联系人中
Store hidden sync identifier in android raw contact
我想创建一个从给定服务器同步联系人的应用程序。为了识别这些联系人,我想在隐藏字段中的 android 联系人中存储一个标识符。
我找到了似乎适合该字段的字段 SYNC1,但我不知道如何正确使用该字段。
我试过如下设置:
ArrayList<ContentProviderOperation> ops =
new ArrayList<>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, ACCOUNT_NAME)
.withValue(ContactsContract.RawContacts.SYNC1, "myInternalId")
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.RawContacts.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Test User")
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
现在的问题是,批量插入运行通过,但是联系人中没有设置该字段,因为之后我尝试读取它时它是空的:
ContentResolver cr = this.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
String sync1 = null;
int sync1Index = cur.getColumnIndex(ContactsContract.RawContacts.SYNC1);
if (sync1Index >= 0) {
sync1 = cur.getString(sync1Index);
}
System.out.println("Contact id=" + id + " name=" + name + " sync1=" + sync1);
}
}
cur.close();
如果我想在原始联系人中设置它,那么在第二批中,我得到一个异常。
您插入值的方式似乎是正确的,但读取值的方法却不正确。
您无法查询 Contacts.CONTENT_URI
并从 RawContacts
table 中获取字段。
要读回您的值,试试这个:
ContentResolver cr = this.getContentResolver();
String[] projection = new String[] { RawContacts._ID, RawContacts.CONTACT_ID, RawContacts.SYNC1 }
String selection = RawContacts.ACCOUNT_TYPE + " = '" + accountType + "' AND " + RawContacts.ACCOUNT_NAME + " = '" + accountName + "'";
Cursor cur = cr.query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, null, null);
while (cur.moveToNext()) {
Long rawId = cur.getLong(0);
Long contactId = cur.getLong(1);
String sync1 = cur.getString(2);
System.out.println("Contact id=" + contactId + " raw-id=" + rawId + " sync1=" + sync1);
}
cur.close();
我想创建一个从给定服务器同步联系人的应用程序。为了识别这些联系人,我想在隐藏字段中的 android 联系人中存储一个标识符。
我找到了似乎适合该字段的字段 SYNC1,但我不知道如何正确使用该字段。
我试过如下设置:
ArrayList<ContentProviderOperation> ops =
new ArrayList<>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, ACCOUNT_NAME)
.withValue(ContactsContract.RawContacts.SYNC1, "myInternalId")
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.RawContacts.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Test User")
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
现在的问题是,批量插入运行通过,但是联系人中没有设置该字段,因为之后我尝试读取它时它是空的:
ContentResolver cr = this.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
String sync1 = null;
int sync1Index = cur.getColumnIndex(ContactsContract.RawContacts.SYNC1);
if (sync1Index >= 0) {
sync1 = cur.getString(sync1Index);
}
System.out.println("Contact id=" + id + " name=" + name + " sync1=" + sync1);
}
}
cur.close();
如果我想在原始联系人中设置它,那么在第二批中,我得到一个异常。
您插入值的方式似乎是正确的,但读取值的方法却不正确。
您无法查询 Contacts.CONTENT_URI
并从 RawContacts
table 中获取字段。
要读回您的值,试试这个:
ContentResolver cr = this.getContentResolver();
String[] projection = new String[] { RawContacts._ID, RawContacts.CONTACT_ID, RawContacts.SYNC1 }
String selection = RawContacts.ACCOUNT_TYPE + " = '" + accountType + "' AND " + RawContacts.ACCOUNT_NAME + " = '" + accountName + "'";
Cursor cur = cr.query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, null, null);
while (cur.moveToNext()) {
Long rawId = cur.getLong(0);
Long contactId = cur.getLong(1);
String sync1 = cur.getString(2);
System.out.println("Contact id=" + contactId + " raw-id=" + rawId + " sync1=" + sync1);
}
cur.close();