Android:如何以编程方式获取配置的电子邮件帐户地址

Sud*_*han 2 email account android email-client accountmanager

我使用以下代码来获取配置的帐户名称

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

        Log.d("Account", "Name " + account.name);

        }
Run Code Online (Sandbox Code Playgroud)

但我需要配置的Microsoft Exchange帐户电子邮件ID,因为我们可以更改帐户的名称(它不需要是唯一的).

提前致谢

kri*_*nan 8

此代码正常工作

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) ;
     }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

@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();

}
Run Code Online (Sandbox Code Playgroud)

  • 这段代码非常适合低于 api 24 .. 但高于 api 24 google 帐户没有得到 .. 我已经获得了帐户许可。你知道如何获得 api 24 以上的谷歌账户吗? (2认同)