What is the default Account Type / Name for contacts on Android Contact Application?

I couldn’t find the way to get the SIM account yet.
But I’m using the code below to get the default account name and type.

public void getDefaultAccountNameAndType() {
    String accountType = "";
    String accountName = "";

    long rawContactId = 0;
    Uri rawContactUri = null;
    ContentProviderResult[] results = null;

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 

    ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI).withValue(RawContacts.ACCOUNT_NAME, null).withValue(RawContacts.ACCOUNT_TYPE, null).build());

    try {
        results = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch(Exception e) {
        e.printStackTrace();
    } finally {
        ops.clear();
    }

    for (ContentProviderResult result : results) {
        rawContactUri = result.uri;
        rawContactId = ContentUris.parseId(rawContactUri);
    }

    Cursor c = getContentResolver().query(
            RawContacts.CONTENT_URI
            , new String[] {RawContacts.ACCOUNT_TYPE, RawContacts.ACCOUNT_NAME}
            , RawContacts._ID+"=?"
            , new String[] {String.valueOf(rawContactId)}
            , null);

    if(c.moveToFirst()) {
        if(!c.isAfterLast()) {
            accountType = c.getString(c.getColumnIndex(RawContacts.ACCOUNT_TYPE));
            accountName = c.getString(c.getColumnIndex(RawContacts.ACCOUNT_NAME));
        }
    }

    getContentResolver().delete(rawContactUri, null, null);

    c.close();
    c = null;

    preference.setString("contactAccountType", accountType);
    preference.setString("contactAccountName", accountName);
}

Leave a Comment