2

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

Sudarshan
  • 1,291
  • 3
  • 26
  • 35

4 Answers4

8

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) ;
     }
}
msrd0
  • 7,816
  • 9
  • 47
  • 82
krishnan muthiah pillai
  • 2,711
  • 2
  • 29
  • 35
  • Here the full source code [Android Example.com](http://androidexample.com/Get_Registered_Email_Accounts_-_Android_Example/index.php?view=article_discription&aid=110&aaid=132) – krishnan muthiah pillai Oct 09 '15 at 08:08
6
@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();

}
Rashid Ali
  • 561
  • 6
  • 14
4

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" />
Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
  • This wont work for native email app which allows Microsoft exchange accounts. Native email app allows user to change the name – Sudarshan Oct 18 '13 at 12:35
  • may be this code is useful for you....... Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("message/rfc822"); PackageManager pkgManager = context.getPackageManager(); List activities = pkgManager.queryIntentActivities(intent, 0); – Bhanu Sharma Oct 18 '13 at 12:40
  • 1
    How this could be useful for getting the Email id? – Sudarshan Oct 18 '13 at 12:53
0

Try this code, sure will work for you

AccountManager accManager = AccountManager.get(getApplicationContext());
Account acc[] = accManager.getAccountsByType("com.google");
int accCount = acc.length;

for(int i = 0; i < accCount; i++)
{
    //Do your task here...            
    Toast.makeText(getApplicationContext(),acc[i].name,Toast.LENGTH_SHORT).show();
}
Opal
  • 81,889
  • 28
  • 189
  • 210
user3383787
  • 68
  • 1
  • 2
  • 6