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

public static Account getAccount(Context context, String type) {
    Account[] accountsByType = AccountManager.get(context.getApplicationContext()).getAccountsByType(type);
    return (accountsByType == null || accountsByType.length <= 0) ? null : accountsByType[0];
}

From source file:Main.java

public static boolean isAccountExists(Context context) {
    AccountManager manager = AccountManager.get(context);
    Account[] accounts = manager.getAccountsByType(ACCOUNT_TYPE);
    return accounts.length > 0;
}

From source file:Main.java

public static String getUserEmailOnDevice(Context ctx) {
    AccountManager manager = AccountManager.get(ctx);
    Account[] accounts = manager.getAccountsByType("com.google");
    List<String> possibleEmails = new LinkedList<String>();

    for (Account account : accounts) {
        // TODO: Check possibleEmail against an email regex or treat
        // account.name as an email address only for certain account.type
        // values.
        possibleEmails.add(account.name);
    }/*from  www  . j ava2s . c o m*/

    if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
        String email = possibleEmails.get(0);
        String[] parts = email.split("@");
        if (parts.length > 0 && parts[0] != null)
            return email;
        else
            return null;
    } else
        return null;
}

From source file:Main.java

public static Account findGoogleAccount(Context context, String name) {
    AccountManager am = AccountManager.get(context);
    for (final Account a : am.getAccountsByType(GOOGLE_TYPE)) {
        if (TextUtils.equals(a.name, name))
            return a;
    }/* www. j  av a  2  s .c  om*/
    return null;
}

From source file:Main.java

public static List<Account> getGoogleAccounts(Context context) {
    List<Account> list = new ArrayList<Account>();
    Account[] accounts = AccountManager.get(context).getAccounts();
    for (int i = 0; i < accounts.length; i++) {
        Account account = accounts[i];/*  ww w. j a  va 2 s .  co m*/
        if (account.type.equals("com.google")) {
            list.add(account);
        }
    }
    return list;
}

From source file:Main.java

public static Account getAccount(Context context, String accountName) {
    AccountManager accountManager = AccountManager.get(context);
    Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
    for (Account account : accounts) {
        if (account.name.equalsIgnoreCase(accountName)) {
            return account;
        }//  w  w w  .  ja va  2  s.c om
    }
    return null;
}

From source file:Main.java

public static String getPrimaryAccountEmail(Context context) {
    AccountManager accountManager = AccountManager.get(context);
    Account[] accounts = accountManager.getAccountsByType("com.google");
    if (accounts == null || accounts.length == 0) {
        Log.w(LOG_PREFIX, "Could not find name of primary google account. Returning null");
        return null;
    } else {//from www. ja  v  a  2 s.  c  om
        String name = accounts[0].name;
        Log.d(LOG_PREFIX, "Found " + accounts.length + " google accounts. Returning name:" + name);
        return name;
    }
}

From source file:Main.java

public static Account getAccount(final Context context, final String accountName) {
    final AccountManager manager = AccountManager.get(context);
    Account[] accounts = manager.getAccountsByType(GOOGLE_ACCOUNT_TYPE);
    for (Account account : accounts) {
        if (account.name.equals(accountName)) {
            return account;
        }/*from w w w  .  java  2 s  .  c o m*/
    }
    return null;
}

From source file:Main.java

public static void getEmails(Context context) {
    Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
    Account[] accounts = AccountManager.get(context.getApplicationContext()).getAccounts();
    String possibleEmails;/*from  w w w.j  ava2  s  .  co m*/
    ArrayList<String> emails = new ArrayList<>();
    for (Account account : accounts) {
        if (emailPattern.matcher(account.name).matches()) {
            possibleEmails = account.name;
            emails.add(possibleEmails);
        }
    }
}

From source file:Main.java

public static List<Account> getAccounts(final Context context) {
    AccountManager accountManager = AccountManager.get(context);
    return Arrays.asList(accountManager.getAccountsByType(GOOGLE_ACCOUNT_TYPE));
}