Get User name : Account « Core Class « Android






Get User name

    
//package com.fedorvlasov.lazylist;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;

class Utils {
  private static Account getAccount(AccountManager accountManager) {
    Account[] accounts = accountManager.getAccountsByType("com.google");
    Account account;
    if (accounts.length > 0) {
      account = accounts[0];
    } else {
      account = null;
    }
    return account;
  }

  public String getEmail(Context context) {
    AccountManager accountManager = AccountManager.get(context);
    Account account = getAccount(accountManager);

    if (account == null) {
      return null;
    } else {
      return account.name;
    }
  }

  public String getUsername(Context context) {
    // String email;
    AccountManager manager = AccountManager.get(context);
    Account account = getAccount(manager);
    if (account == null) {
      return "";
    } else {
      String email = account.name;
      String[] parts = email.split("@");
      if (parts.length > 0 && parts[0] != null)
        return parts[0];
      else
        return "";
    }
  }
}

   
    
    
    
  








Related examples in the same category

1.Using AccountManager