How to tell if AccountManager Account supports contacts

247 views Asked by At

Is there a way to determine if an Account returned from AccountManager supports contacts or calendar events?

I see there is something called AccountManagerFeature but I'm not seeing a general way to use that.

In short, what I want is to present a list of accounts to the user when creating a new event or a new contact. The list should only show accounts that are valid to store events and contacts, respectively.

1

There are 1 answers

2
isma3l On

I have done this following this answer: What features are supported by Android's Google accounts authenticator?

Don't know how up to date are the codes, but the ones you need are working.

private static final String ACCOUNT_TYPE_GOOGLE = "com.google";
private static final String[] FEATURES = { "service_mail","service_cl","service_sitemaps" };

private void testGetAccountsByTypeAndFeatures() {
    AccountManagerFuture<Account[]> accounts = AccountManager.get(this).getAccountsByTypeAndFeatures(ACCOUNT_TYPE_GOOGLE, FEATURES, new AccountManagerCallback<Account[]>() {
        @Override
        public void run(AccountManagerFuture<Account[]> future) {
            try {
                for (Account account : future.getResult()) {
                    Log.d("ACCOUNT",account.toString());
                }
            } catch (OperationCanceledException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (AuthenticatorException e) {
                e.printStackTrace();
            }
        }
    }, null);
}

Don't forget to add the permission

<uses-permission android:name="android.permission.GET_ACCOUNTS" />