1

I am working on registration screen on my app. I need to get the email suggestion while typing. It doesn't need to sync with Gmail Id accounts. It should get the email id which I am using as account which we can see in account settings.

Asthme
  • 5,163
  • 6
  • 47
  • 65

1 Answers1

1

User should have Gmail account in his android phone

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

/**
 * This class uses the AccountManager to get the primary email address of the
 * current user.
 */
public class UserEmailFetcher {

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

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

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

In Manifest

  <uses-permission android:name="android.permission.GET_ACCOUNTS" />

Source: https://stackoverflow.com/a/2556540/950427

Community
  • 1
  • 1
Bharath Mg
  • 1,117
  • 1
  • 9
  • 18