get Account Name from AccountManager - Android Account

Android examples for Account:Account Name

Description

get Account Name from AccountManager

Demo Code


import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;

public class Main{
    public static String getAccountName(Context context) {
        Account account = AccountUtils.getAccount(context, "com.google", 0);

        if (account != null) {
            return account.name;
        } else {/*  w  ww. ja v a2  s . co  m*/
            return "Guest";
        }
    }
    public static Account getAccount(Context context, String pattern, int i) {
        AccountManager accountManager = AccountManager.get(context);

        if (accountManager != null) {
            Account[] accounts = accountManager.getAccountsByType(pattern);

            if (accounts != null && accounts.length > i
                    && accounts[i] != null) {
                return accounts[i];
            }
        }

        return null;
    }
}

Related Tutorials