is Email From Accounts - Android Account

Android examples for Account:Email Address

Description

is Email From Accounts

Demo Code


//package com.java2s;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;

import android.util.Patterns;
import java.util.HashSet;

import java.util.Set;

public class Main {
    public static boolean isEmailFromAccounts(Context context, String email) {
        for (String curEmail : getAllAccountEmails(context)) {
            if (curEmail.equals(email)) {
                return true;
            }//from  www  .j a  v a 2  s. c  o  m
        }
        return false;
    }

    /**
     * Gets array of found e-mails from AccountManager
     * @return e-mail string array (length()==0 if no email accounts were found)
     */
    public static String[] getAllAccountEmails(Context context) {
        Set<String> result = new HashSet<String>();
        Account[] accounts = AccountManager.get(context).getAccounts();
        for (Account acc : accounts) {
            if (Patterns.EMAIL_ADDRESS.matcher(acc.name).matches()) {
                result.add(acc.name);
            }
        }
        return result.toArray(new String[result.size()]);
    }
}

Related Tutorials