Example usage for android.accounts AccountManager get

List of usage examples for android.accounts AccountManager get

Introduction

In this page you can find the example usage for android.accounts AccountManager get.

Prototype

public static AccountManager get(Context context) 

Source Link

Document

Gets an AccountManager instance associated with a Context.

Usage

From source file:Main.java

/**
 * Get all registered accounts from system
 * /* ww w . j a v  a 2s.  c o  m*/
 * @param context
 * @return array of accounts
 */
public static Account[] getAccounts(final Context context, String accountType) {
    return AccountManager.get(context).getAccountsByType(accountType);
}

From source file:Main.java

/**
 * Remove specified account/* w  w w.  j  a va 2s .c om*/
 * @param context context
 * @param account account to remove
 */
public static void removeAccount(Context context, Account account) {
    final AccountManager accountManager = AccountManager.get(context);
    accountManager.removeAccount(account, null, null);
}

From source file:Main.java

static List<String> getEmailAddresses(Context context, int limit) {
    ArrayList<String> emails = new ArrayList<String>();
    AccountManager am = AccountManager.get(context);
    if (am == null) {
        return emails;
    }//from   w  ww  .  j av a2s.c om
    Account[] accounts = am.getAccountsByType("com.google");
    if (accounts == null || accounts.length == 0) {
        return emails;
    }
    for (Account a : accounts) {
        if (a.name == null || a.name.length() == 0) {
            continue;
        }
        emails.add(a.name.trim().toLowerCase());
    }
    while (emails.size() > limit) {
        emails.remove(emails.size() - 1);
    }
    return emails;
}

From source file:Main.java

public static String getEmailAccount(Context context) {
    try {/*from   w w  w.j  av  a 2s.  c om*/
        Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
        Account[] accounts = AccountManager.get(context).getAccounts();
        for (Account account : accounts) {
            if (emailPattern.matcher(account.name).matches()) {
                String possibleEmail = account.name;
                return possibleEmail;
            }
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:Main.java

/**
 * Get password of specified account/* ww  w  . j a  v a  2  s. c om*/
 * @param context context
 * @param account account
 * @return account password
 */
public static String getAccountPassword(Context context, Account account) {
    if (account == null)
        return null;
    AccountManager accountManager = AccountManager.get(context);
    return accountManager.getPassword(account);
}

From source file:Main.java

/**
 * Get account by type and name/*  www. j a v a2s .c o  m*/
 * @param context context
 * @param accountType account type
 * @param accountName account name
 * @return Account object
 */
public static Account getAccount(Context context, String accountType, String accountName) {
    final AccountManager accountManager = AccountManager.get(context);
    Account[] accounts = accountManager.getAccountsByType(accountType);

    if (accounts == null || accounts.length == 0) {
        return null;
    } else {
        for (Account account : accounts) {
            if (account.name.equals(accountName)) {
                return account;
            }
        }
        return null;
    }
}

From source file:Main.java

/**
 * Get user data of specified account// w  w w  .  ja  v a2  s .c om
 * @param context context
 * @param account account
 * @param key key
 * @return user data string
 */
public static String getUserData(Context context, Account account, String key) {
    if (account == null)
        return null;
    AccountManager accountManager = AccountManager.get(context);
    return accountManager.getUserData(account, key);
}

From source file:Main.java

public static List<String> getUserEmailAccounts(Context context) {
    // get all accounts listed in the phone
    List<String> accountSet = new ArrayList<>();
    // match with email address pattern and add to set to avoid duplicates
    Account[] accounts = AccountManager.get(context).getAccounts();
    for (Account account : accounts) {
        if (Patterns.EMAIL_ADDRESS.matcher(account.name).matches()) {
            accountSet.add(account.name);
        }//  w ww .j  a v  a2 s. c  om
    }
    return accountSet;
}

From source file:Main.java

/**
 * Set user data to account/*from   w w w .j  a  v a 2 s .c  o m*/
 * @param context context
 * @param account account
 * @param key key
 * @param value value
 */
public static void setUserData(Context context, Account account, String key, String value) {
    if (account == null)
        throw new IllegalArgumentException("account cannot be null");
    AccountManager accountManager = AccountManager.get(context);
    accountManager.setUserData(account, key, value);
}

From source file:Main.java

public static void removeActiveAccount(Context context, String accountType) {
    Account account = getActiveAccount(context, accountType);
    if (account != null) {
        AccountManager.get(context).removeAccount(account, null, null);
    }/*from   ww w . j  a  v  a2  s. c  o m*/
    setActiveAccountName(context, null);
}