Gets array of found e-mails from AccountManager - Android java.lang

Android examples for java.lang:Array Element

Description

Gets array of found e-mails from AccountManager

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 {
    /**//  w ww  .  j a v a 2  s . c o  m
     * 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