Search code examples
androidsonysony-xperia

Android, Insert RawContact not showing on Sony Xperia


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.


Solution

  • 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");