Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. ArrayList<ContentProviderOperation> ops =
  2. new ArrayList<>();
  3.  
  4. int rawContactInsertIndex = ops.size();
  5. ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
  6. .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
  7. .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, ACCOUNT_NAME)
  8. .withValue(ContactsContract.RawContacts.SYNC1, "myInternalId")
  9. .build());
  10.  
  11. ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
  12. .withValueBackReference(ContactsContract.RawContacts.Data.RAW_CONTACT_ID, rawContactInsertIndex)
  13. .withValue(ContactsContract.RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
  14. .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Test User")
  15. .build());
  16.  
  17. getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
  18.  
  19. ContentResolver cr = this.getContentResolver();
  20. Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
  21.  
  22. if (cur.getCount() > 0) {
  23. while (cur.moveToNext()) {
  24. String id = cur.getString(cur.getColumnIndex(
  25. ContactsContract.Contacts._ID));
  26. String name = cur.getString(cur.getColumnIndex(
  27. ContactsContract.Contacts.DISPLAY_NAME));
  28.  
  29. String sync1 = null;
  30. int sync1Index = cur.getColumnIndex(ContactsContract.RawContacts.SYNC1);
  31. if (sync1Index >= 0) {
  32. sync1 = cur.getString(sync1Index);
  33. }
  34.  
  35. System.out.println("Contact id=" + id + " name=" + name + " sync1=" + sync1);
  36. }
  37. }
  38.  
  39. cur.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement