Using AccountManager : Account « Core Class « Android






Using AccountManager

    
//package gr.atc.epart;

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

class AccountUtil {
  public static String getAccountName(Context context) {
    String email = getEmail(context);
    String name = getNameFromEmail(email);
    return name;
  }

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

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

  public 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;
  }

  private static String getNameFromEmail(String email) {

    if (email==null) {
      return null;
    }
    
    String name = "";
    int i = email.lastIndexOf('@');

    if (i > 0 && i < email.length() - 1) {

      name = email.substring(0, i);
    }
    return name;

  }
}

   
    
    
    
  








Related examples in the same category

1.Get User name