Android: How to get the configured email account address programmatically

21,614

Solution 1

This code work properly

public class RegisteredEmailAccounts extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.registered_email_account);
    final TextView accountsData = (TextView) findViewById(R.id.accounts);

      String possibleEmail="";

       try{
               possibleEmail += "************* Get Registered Gmail Account 
                                  *************\n\n";
               Account[] accounts =  
           AccountManager.get(this).getAccountsByType("com.google");

               for (Account account : accounts) {

                 possibleEmail += " --> "+account.name+" : "+account.type+" , \n";
                 possibleEmail += " \n\n";

               }
          }
          catch(Exception e)
          {
               Log.i("Exception", "Exception:"+e) ; 
          }


          try{
               possibleEmail += "**************** Get All Registered Accounts 
                      *****************\n\n";

               Account[] accounts = AccountManager.get(this).getAccounts();
               for (Account account : accounts) {

                  possibleEmail += " --> "+account.name+" : "+account.type+" , \n";
                  possibleEmail += " \n";

               }
          }
          catch(Exception e)
          {
               Log.i("Exception", "Exception:"+e) ; 
          }

       // Show on screen    
       accountsData.setText(possibleEmail);

       Log.i("Exception", "mails:"+possibleEmail) ;
     }
}

Solution 2

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);


    String gmail = null;

    Pattern gmailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
    Account[] accounts = AccountManager.get(this).getAccounts();
    for (Account account : accounts) {
        if (gmailPattern.matcher(account.name).matches()) {
             gmail = account.name;
        }
    }

    Toast.makeText(this, gmail, Toast.LENGTH_LONG).show();

}

Solution 3

i think this code will be helpful for you dear.

Here is my code:

AccountManager accManager = AccountManager.get(context);
Account acc[] = accManager.getAccounts();
int accCount = acc.length;
AppConstants.accOnDevice = new Vector<String>();
for(int i = 0; i < accCount; i++){
//Do your task here...
}

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
Share:
21,614
Sudarshan
Author by

Sudarshan

Updated on July 10, 2022

Comments

  • Sudarshan
    Sudarshan almost 2 years

    I used the below code to get the configured account name

    Account[] accounts = AccountManager.get(this).getAccounts();
            for (Account account : accounts) {
    
            Log.d("Account", "Name " + account.name);
    
            }
    

    But i need the email id of the configured Microsoft Exchange account as we can change the name of the account (it is not need to be unique).

    Thanks in Advance