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

落花浮王杯 提交于 2019-11-27 14:24:06
Christoph Haefner

I made the same experiences as you did and can only suggest two workarounds:

  1. Let the user make the decision. For example show him a list of all raw contacts and then let him choose which one is a phonebook contact vs. a sim contact.

  2. My experience with three different devices is that the AccountManager is not aware of the account used to store those phonecontacts. For example when you fetch an account array from the AccountManager as you did (AccountManager.getAccounts()) the "com.htc.android.pcsc" is not in the list! But you can use exactly that fact to your advantage: Exclude all known account types/names and the list you get should be the list of all phonebookcontacts/simcontacts.

Hopefully those ideas helped you :) I would like to read your thoughts about those workarounds, eventually I missed something or there is a even better workaround.

Brad Hong

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);
}

I solved this problem.

Account[] accountList = AccountManager.get(this).getAccounts();

String accountSelection = "";
for(int i = 0 ; i < accountList.length ; i++) {
  if(accountSelection.length() != 0)
    accountSelection = accountSelection + " AND ";
  accountSelection = accountSelection + ContactsContract.Groups.ACCOUNT_TYPE + " != '" +  accountList[i].type + "'";
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!