Search code examples
javaandroidaccountmanager

Checking to see if getAccountsByType() found an account


I am working on implementing a custom account type for AccountManager. I am looking for an account with the following code:

AccountManager am = AccountManager.get(this); 
Account[] accounts = am.getAccountsByType("com.mycustomtype");

I'm new to this and don't know how to process the result. I basically want an if statement that sets a variable to 0 if no account is found, and otherwise sets a username variable. This way I know whether to start a loginActivity or my mainActivity.

How do I process the result of AccountManager in this way?


Solution

  • I got this working using array.length as follows:

    private boolean accountExists = false;
    
        AccountManager am = AccountManager.get(this); 
    Account[] accounts = am.getAccountsByType("com.mycustomtype");
    if(accounts.length >= 1) {
        accountExists = true;
    }