Visit Sponsor

Written by 5:08 pm Android

Retrieving All Registered Email Accounts in Android

Overview

Android devices may contain multiple user accounts such as Google accounts, Exchange accounts, and other account types managed by Android’s Account Manager. Retrieving a list of registered email accounts can be useful in scenarios such as pre-filling user profiles, offering sign-in options, or tailoring app behavior based on the user’s existing accounts.

This guide explains how to access registered accounts on an Android device using the AccountManager API and how to filter email-type accounts safely and responsibly.

Permissions and Privacy

Accessing account information involves personal and sensitive user data. Android enforces restrictions depending on the target API level:

  • On Android 6.0 and below, the GET_ACCOUNTS permission must be declared in the manifest.
  • On Android 8.0 (API level 26) and above, access to accounts depends on visibility rules set by the account authenticator or the user, and GET_ACCOUNTS may no longer provide access without appropriate visibility configuration.

Always disclose how and why your app accesses account data to comply with Play Store policies and privacy expectations.

Declare Permissions (Legacy)

For older devices (API < 26), include the following permission in your app’s manifest:

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

This allows your application to request access to account lists managed by the system.

Using AccountManager to Retrieve Accounts

Android provides the AccountManager class to list all accounts or accounts of a specific type registered on the device.

Initialize AccountManager

AccountManager accountManager = AccountManager.get(context);

This provides access to the device’s account registry.

Retrieving All Accounts

You can retrieve all accounts visible to your app using:

Account[] allAccounts = accountManager.getAccounts();

Each Account object contains a name (often an email address) and a type identifying the authenticator (Google, Exchange, etc.).

Filtering for Email-Like Accounts

To extract valid email addresses from the account list, you can validate them using Android’s built-in email pattern matching:

Pattern emailPattern = Patterns.EMAIL_ADDRESS;
List<String> emailList = new ArrayList<>();

for (Account account : allAccounts) {
    if (emailPattern.matcher(account.name).matches()) {
        emailList.add(account.name);
    }
}

This approach ensures that you collect only accounts with values resembling email addresses, such as those from Google or other services.

Retrieving Only Google Accounts

If your use case targets Google accounts specifically, use:

Account[] googleAccounts = accountManager.getAccountsByType("com.google");

The "com.google" type corresponds to Google’s account authenticator. These accounts often represent Google services including Gmail and Play Services.

Sample Implementation

Below is a typical activity implementation combining account retrieval and email pattern filtering:

import android.app.Activity;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.os.Bundle;
import android.util.Patterns;
import android.widget.TextView;

public class RegisteredAccountsActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registered_accounts);

        TextView outputView = findViewById(R.id.output);
        AccountManager accountManager = AccountManager.get(this);
        
        StringBuilder result = new StringBuilder();
        Account[] accounts = accountManager.getAccounts();

        for (Account account : accounts) {
            if (Patterns.EMAIL_ADDRESS.matcher(account.name).matches()) {
                result.append("Account: ").append(account.name)
                      .append(" (Type: ").append(account.type).append(")\n");
            }
        }

        outputView.setText(result.toString());
    }
}

In this example:

  • Accounts registered on the device are retrieved.
  • Only entries that match a valid email pattern are retained and displayed.

Handling Runtime Permissions

Starting from Android 6.0 (API level 23), you should request runtime permissions if your app declares GET_ACCOUNTS:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS)
        != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.GET_ACCOUNTS}, REQUEST_CODE);
}

Handle the user’s response in onRequestPermissionsResult accordingly before accessing accounts.

Best Practices and Privacy

  • Only access account data when essential for app functionality.
  • Clearly explain in your privacy policy how this data is used.
  • Avoid collecting unnecessary account information or sharing it externally.

Remember that modern authentication flows (e.g., Google Identity Services) encourage user-initiated sign-in rather than silent account enumeration to improve user trust and privacy.

Visited 5 times, 2 visit(s) today
Close