Returns the user name of primary google account. - Android Account

Android examples for Account:Account Name

Description

Returns the user name of primary google account.

Demo Code


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

public class Main {
    /**/*  w w w. j ava  2  s  . c  o m*/
     * Returns the user name of primary google account.
     *
     * @return
     */
    public static String getPrimaryUserID(Context context) {
        AccountManager manager = AccountManager.get(context);
        Account[] accounts = manager.getAccountsByType("com.google");
        List<String> possibleEmails = new LinkedList<String>();

        for (Account account : accounts) {
            // TODO: Check possibleEmail against an email regex or treat
            // account.name as an email address only for certain account.type
            // values.
            possibleEmails.add(account.name);
        }

        if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
            return possibleEmails.get(0);
        } else
            return null;
    }
}

Related Tutorials