0

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.

Peri Hartman
  • 19,314
  • 18
  • 55
  • 101

1 Answers1

0

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" />
Community
  • 1
  • 1
isma3l
  • 3,833
  • 1
  • 22
  • 28
  • It seems this would work only for google accounts. What about other kinds of accounts, such as facebook? They can use their own feature string, correct? – Peri Hartman Jun 23 '15 at 19:59
  • Yes, they would use their own string for that because they could have their own features. I cant't find other documentation, sorry. – isma3l Jun 23 '15 at 20:07