问题
I use following code for new contacts added to phone.
private static void addContact(Account account, String name, String username,String phone,String email) {
Log.i(TAG, "Adding contact: " + name);
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
builder.withValue(RawContacts.SYNC1, username);
operationList.add(builder.build());
//NAME adding area
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());
// Phone Number adding area
builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
builder.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, 0);
builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone);
builder.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
operationList.add(builder.build());
}
This code work fine for adding a new contact.
I want to update my contacts from server to phone. So I need contact id for updating purpose.
Can I get contact id from above code?. If anybody known share your answer.
If any questions or comments also welcome. Thanks for your answers.
回答1:
Try this code,
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Uri myContactUri = res[0].uri;
int lastSlash = myContactUri.toString().lastIndexOf("/");
int length = myContactUri.toString().length();
int contactID = Integer.parseInt((String) myContactUri.toString().subSequence(lastSlash+1, length));
I hope this code help you..
回答2:
The bulk insert that you do with the operation list will return a list of results (URIs I think, but it's been a few months since I did this), I believe you can get the ID you want from the first of these results
long rawContactId = ContentUris.parseId(rawContactUri);
回答3:
After Applying the badge and receive the last segment you receive: ContactsContract.RawContacts._ID If you want to get Contact_id you should make this query:
(rowId - is the last segment you received before)
String[] projections = new String[]{ContactsContract.RawContacts.CONTACT_ID};
String select = ContactsContract.RawContacts._ID + "= ?";
String[] selectionArgs = new String[]{rowId};
Cursor cursor = context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projections, select, selectionArgs, null);
String contactId = null;
if (cursor.getCount() == 0) {
Log.d("aaa","Couldn't find row: " + rowId);
return null;
}
if (cursor.moveToNext()) {
int columnIndex;
columnIndex = cursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID);
if (columnIndex == -1) {
Log.d("aaa","Couldn't get contact id");
} else {
contactId = cursor.getString(columnIndex);
}
}
cursor.close();
return contactId;
来源:https://stackoverflow.com/questions/24745320/how-to-get-contact-id-after-add-a-new-contact-in-android