0

I'm using ContactsContract to insert RawContacts in my app. The following code (and showing the inserted contact in the contact app) works on all devices, but not on Sony Xperia (Android 4.4.4).

ContentValues p=new ContentValues();
p.put(ContactsContract.RawContacts.ACCOUNT_TYPE, getActivity().getPackageName());
p.put(ContactsContract.RawContacts.ACCOUNT_NAME,     
DataHelper.getAppName(getActivity()));
Uri rowcontact = null;
long rawcontactid = 0;
try {
  rowcontact = getActivity().getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, p);
  rawcontactid = ContentUris.parseId(rowcontact);
  Log.d(DEBUG_KEY, "CONTACT ADDED: " + rawcontactid);
}catch(Exception e){
  Log.d(DEBUG_KEY, "CONTACT ADDED FAILED 1: " + e.getMessage());
  return "";
}

On the Sony Xperia Device there is no error or exception. The console display the correct CONTACT-ADDED-ID. But in the android contact app the newly inserted (Raw)-Contact is not showing. I have enabled all groups, etc. in the filter-settings in the contact app.

  • what is the purpose of setting `ACCOUNT_TYPE` to `getActivity().getPackageName()` ? – pskink Sep 15 '15 at 07:03
  • From the google documentation: To ensure uniqueness, new account types should be chosen according to the Java package naming convention. Thus a Google account is of type "com.google" http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html – appagenturbw Sep 15 '15 at 07:33
  • but you want to add a contact to "a Google account", don't you (so that contact app can see it)? – pskink Sep 15 '15 at 07:36

1 Answers1

0

Ok. Problem is solved. On Sony Xperia Devices you must

(1) Specify an existing (google) account

p.put(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google");
p.put(ContactsContract.RawContacts.ACCOUNT_NAME, "google_account_username_on_device");

or (2) don't specify any account type details

//p.put(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google");
//p.put(ContactsContract.RawContacts.ACCOUNT_NAME, "google_account_username_on_device");

On other devices like Motorola oder Google Nexus you can specify a non existing, custom Account, like:

p.put(ContactsContract.RawContacts.ACCOUNT_TYPE, "my.app.name");
p.put(ContactsContract.RawContacts.ACCOUNT_NAME, "APP NAME");
  • This sucks bigtime. When I want to use my custom account: contacts are not shown and the name can't be saved. (only at Sony's) – Roel Dec 30 '15 at 08:38