get Binded Email Address from Context - Android Account

Android examples for Account:Email Address

Description

get Binded Email Address from Context

Demo Code


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

public class Main {
    public static String getBindedEmailAddress(Context context) {
        String retStr = "";
        if (context != null) {
            try {
                AccountManager am = AccountManager.get(context);
                Account[] accs = am.getAccounts();
                String google = "";
                String email = "";
                String other = "";
                for (Account a : accs) {
                    if (a.type.equals("com.google")) {
                        google = a.name;
                    } else if (a.type.equals("com.android.email")) {
                        email = a.name;/*from  w  w w  .  j av a2s.  com*/
                    } else {
                        if (isEmail(a.name)) {
                            other = a.name;
                        }
                    }
                }

                if (!google.equals("")) {
                    retStr = google;
                }
                if (retStr.equals("") && !email.equals("")) {
                    retStr = email;
                }
                if (retStr.equals("")) {
                    retStr = other;
                }

            } catch (Exception e) {

            }
        }
        return retStr;
    }

    private static boolean isEmail(String text) {
        String reg = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern regex = Pattern.compile(reg);
        Matcher matcher = regex.matcher(text);
        return matcher.matches();

    }
}

Related Tutorials