37

I have android.permission.READ_OWNER_DATA but I can't find any reliable code that would explain how can I read email address of device's owner. How can I do this?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
nikib3ro
  • 20,366
  • 24
  • 120
  • 181
  • 1
    See this question: http://stackoverflow.com/questions/2112965/how-to-get-the-android-devices-primary-e-mail-address – Erich Douglass Mar 31 '10 at 21:13
  • 1
    My code is for 2+ Android versions but I hope it will help someone. It gets owner name, email and phone (user may have email besides the syncronization account). Tested on android 2.3 https://gist.github.com/3904299 – Jehy Oct 17 '12 at 08:10
  • 5
    Why are good questions closed? I am starting to see this more and more. – Jared Burrows Nov 07 '13 at 15:43

4 Answers4

91

Why you wanna do that?

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;

/**
 * This class uses the AccountManager to get the primary email address of the
 * current user.
 */
public class UserEmailFetcher {

  static String getEmail(Context context) {
    AccountManager accountManager = AccountManager.get(context); 
    Account account = getAccount(accountManager);

    if (account == null) {
      return null;
    } else {
      return account.name;
    }
  }

  private static Account getAccount(AccountManager accountManager) {
    Account[] accounts = accountManager.getAccountsByType("com.google");
    Account account;
    if (accounts.length > 0) {
      account = accounts[0];      
    } else {
      account = null;
    }
    return account;
  }
}

In your AnroidManifest.xml

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
  • This looks very nice... but I need something that works with Android 1.5? Do you have any class for older API in your sleeve? – nikib3ro Mar 31 '10 at 21:29
  • 2
    This won't work in Android 1.* I'm afraid. If it's any comfort you can still use this in an app that targets 1.5, and cleanly detect when the functionality isn't available with try { String name = UserEmailFetcher.getEmail(this); } catch (VerifyError e) { // Happens if the AccountManager is not available (e.g. 1.x) } You would need in your manifest. – Jim Blackler Mar 31 '10 at 21:36
  • It's hidden, you'll have to use reflection to get it. Don't if you _really_ need to. – alexanderblom Apr 01 '10 at 01:10
  • Its working cool in all android version, but not working on Oreo 8.0, anyone facing same issue and @JimBlackler ? – Mehul Gajjar Jan 07 '19 at 09:07
  • 1
    I m using android 10, the email is null, I don't know why. – GSandro_Strongs Aug 12 '21 at 18:24
  • On Android 11 i am getting gmail id by calling getAccount(accountManager) but it's type is 'com.osp.app.signin' and not 'com.google' – Imran Khan Saifi Jan 19 '23 at 14:48
2

please add this permission in your manifest file.

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

Add this function in your MainActivity.java

    public void getAccounts() {
        StringBuilder emailAccounts = new StringBuilder();

        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.GET_ACCOUNTS)) {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.GET_ACCOUNTS}, 1);
            } else {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.GET_ACCOUNTS}, 1);
            }
        }
        else {
            List<String> accountList = new ArrayList<String>();
            Pattern gmailPattern = Patterns.EMAIL_ADDRESS;
            Account[] accounts = AccountManager.get(this).getAccounts();
            for (Account account : accounts) {
                if (gmailPattern.matcher(account.name).matches()) {
                    emailAccounts.append(flag + ". " + "<b>" + account.name + "<br>" +
                            "---------" + "---------<br>"); ;
                }
            }
            deviceDetails.setText(Html.fromHtml(emailAccounts+""));
        }
    }

now call this function in your onCreate() method like this.

getAccounts();

Zia
  • 705
  • 9
  • 11
  • What's the point of the 2nd `if` structure (with the `ActivityCompat`)? It does the exact same thing in both cases. – juzraai Feb 10 '22 at 08:08
1

Working on MarshMallow Version

btn_click=(Button) findViewById(R.id.btn_click);

btn_click.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            int permissionCheck = ContextCompat.checkSelfPermission(PermissionActivity.this,
                    android.Manifest.permission.CAMERA);
            if (permissionCheck == PackageManager.PERMISSION_GRANTED)
            {
                //showing dialog to select image
                String possibleEmail=null;

                 Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
                 Account[] accounts = AccountManager.get(PermissionActivity.this).getAccounts();
                 for (Account account : accounts) {
                     if (emailPattern.matcher(account.name).matches()) {
                         possibleEmail = account.name;
                         Log.e("keshav","possibleEmail"+possibleEmail);
                     }
                 }

                Log.e("keshav","possibleEmail gjhh->"+possibleEmail);
                Log.e("permission", "granted Marshmallow O/S");

            } else {                        ActivityCompat.requestPermissions(PermissionActivity.this,
                        new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE,
                                android.Manifest.permission.READ_PHONE_STATE,
                                Manifest.permission.GET_ACCOUNTS,
                                android.Manifest.permission.CAMERA}, 1);
            }
        } else {

// Lower then Marshmallow

                String possibleEmail=null;

                 Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
                 Account[] accounts = AccountManager.get(PermissionActivity.this).getAccounts();
                 for (Account account : accounts) {
                     if (emailPattern.matcher(account.name).matches()) {
                         possibleEmail = account.name;
                         Log.e("keshav","possibleEmail"+possibleEmail);
                 }
                Log.e("keshav","possibleEmail gjhh->"+possibleEmail);
        }
    }
});

Community
  • 1
  • 1
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53
-2

I found the solution

startActivityForResult(intent, ANY_CODE);

@Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ANY_CODE&& resultCode == RESULT_OK) {
            String emailAddress = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
            // do something...
        }
    }
Pedro Machado
  • 158
  • 2
  • 10