I have an Android app that is deployed to Android Enterprise users in my company through the managed Play Store. Every user has a work profile on their device. The email address of this account is the user's work email address.
The app needs to access the user's work email address in order to sync user data to the server.
On devices running versions of Android prior to 8 (Oreo), this code works great:
AndroidManifest.xml
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
Code
public String getUserEmail() {
AccountManager manager = AccountManager.get(this);
Account[] accounts = manager.getAccounts();
List<String> emails = new ArrayList<String>();
// On Oreo+, the length of accounts is 0.
for (Account account : accounts) {
if (account.name.endsWith("@companyname.com")) {
emails.add(account.name);
}
}
if (!emails.isEmpty() && emails.get(0) != null) {
return emails.get(0);
}
return null;
}
In Oreo and above, the result of AccountManager.getAccounts() is always of length zero.
This behavior change is documented by Google: (https://developer.android.com/about/versions/oreo/android-8.0-changes.html#aaad). The GET_ACCOUNTS permission is no longer sufficient to retrieve accounts and we have to use AccountManager.newChooseAccountIntent().
This is undesirable because I don't want to let the user choose a non-work account to use with this app. I only want their work email address.
Is there a way for an Android Enterprise app to obtain the user's work email address without using AccountManager.newChooseAccountIntent()? Is there a way for the EMM administrator to make this property available to the application through the Android Enterprise SDK? Or is there another way to obtain the user's work email address?