5

我正在通过此代码保存联系人

ArrayList<ContentProviderOperation> ops =
          new ArrayList<ContentProviderOperation>();
 ...
 int rawContactInsertIndex = ops.size();
 ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
          .withValue(RawContacts.ACCOUNT_TYPE, accountType)
          .withValue(RawContacts.ACCOUNT_NAME, accountName)
          .build());

 ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
          .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
          .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
          .withValue(StructuredName.GIVEN_NAME, linkname1)
          .withValue(StructuredName.FAMILY_NAME, linkname2)
          .build());

 getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

保存联系人后,我需要获取 _ID 字段,以便我可以从通讯录中获取该联系人进行编辑。保存后如何获取id?

提前致谢

4

2 回答 2

3
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

Uri myContactUri = res[0].uri;
int contactID = Integer.parseInt(myContactUri.getLastPathSegment());
于 2013-09-16T13:19:12.783 回答
0

干得好。从联系人中获取号码(“phnumber”)的联系人 ID

String[] projection = new String[]{Contacts._ID};
Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,Uri.encode(phnumber));
Cursor c = getContentResolver().query(contactUri, projection,
  null, null, null);
if (c.moveToFirst()) {
long contactId=c.getColumnIndex(Contacts._ID);


c.close();

}
于 2013-09-16T13:00:05.510 回答