Gets first found e-mail from AccountManager - Android Account

Android examples for Account:Email Address

Description

Gets first found e-mail from AccountManager

Demo Code


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

import android.util.Patterns;

public class Main {
    /**//  www. java 2s  .c  om
     * Gets first found e-mail from AccountManager
     * @return e-mail or empty string
     */
    public static String getFirstAccountEmail(Context context) {
        StringBuilder result = new StringBuilder();
        Account[] accounts = AccountManager.get(
                context.getApplicationContext()).getAccounts();
        for (Account acc : accounts) {
            if (Patterns.EMAIL_ADDRESS.matcher(acc.name).matches()) {
                result.append(acc.name);
                break;
            }
        }
        return result.toString();
    }
}

Related Tutorials