I am integrating my app with android default Contacts application.I would like to show an option "xyz using MyApp" inside every Contacts Detail.I am able to see my app in Accounts Section with an option to sync Contacts but still my app not merging with existing contacts but instead creating a new contact and merging in it.
performSync() method
private static void addContact(ContentResolver contentResolver,int name, int phoneNumber) {
Log.i("BHUVNESH", "Adding contact: " + name);
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
//Create our RawContact
ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI);
builder.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, name);
builder.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.example.bhuvnesh.myapplication");
builder.withValue(ContactsContract.RawContacts.SYNC1, phoneNumber);
operationList.add(builder.build());
//Create a Data record of common type 'StructuredName' for our RawContact
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());
//Create a Data record of custom type "vnd.android.cursor.item/vnd.com.example.bhuvnesh.myapplication.profile" to display a link to the Last.fm profile
builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
builder.withValue(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/vnd.com.example.bhuvnesh.myapplication.profile");
builder.withValue(ContactsContract.Data.DATA1, phoneNumber);
builder.withValue(ContactsContract.Data.DATA2, "Last.fm Profile");
builder.withValue(ContactsContract.Data.DATA3, "View profile");
operationList.add(builder.build());
try {
contentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
} catch (Exception e) {
Log.e("BHUVNESH", "Something went wrong during creation! " + e);
e.printStackTrace();
}
}
Try this here is the working code for me
OUTPUT
UPDATE
in your
addContact
code you're missing the part that tellsContacts DB
to join your new raw-contact into the existing contact, so that contact will now contain your raw-contact and your app-specific line will be shown when opening that contact in the contacts app.Check this answer on how to join a
RawContact
into an existing Contact: why won't contacts aggregate?You'll probably need to pass in some
RawContact ID
to your addContact method so it'll be able to join the two together.UPDATE
Instead of applying the aggregation operation together with your
RawContact
insert operation, let's try to separate into twoapplyBatch
calls, also, let's aggregate your new raw-contact with ALL existing raw-contacts, not just one of them. Try the following code, make sure you pass to it the existing contact-id (not raw-contact id) and your new raw-contact-id.P.S.
don't open two duplicate questions, one is enough.
ANOTHER UPDATE
You seem to have some confusion about IDs.
There are
Data
IDs,RawContact
IDs, andContact
IDs.CommonDataKinds.Phone._ID
will return aData
ID, identifying the specific row in the Data table in which that phone number is stored.You can get a from the
Phone
table the other IDs as well, use:CommonDataKinds.Phone.RAW_CONTACT_ID
CommonDataKinds.Phone.CONTACT_ID
You can read more here: https://stackoverflow.com/a/50084029/819355