get Account Manager Email - Android Account

Android examples for Account:Account Information

Description

get Account Manager Email

Demo Code

//package com.java2s;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.util.Patterns;

public class Main {
    public static String getAccountManagerEmail(final Context context) {
        List<String> emails = getAccountManagerEmails(context);
        if (null == emails || emails.isEmpty()) {
            return null;
        }// ww w  . jav a2  s  .  c o m

        return emails.get(0);
    }

    public static List<String> getAccountManagerEmails(final Context context) {

        final List<String> emails = new LinkedList();

        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;
                emails.add(possibleEmail);
            }
        }

        return emails;
    }
}

Related Tutorials