java – How can you get an Android user's email address?-ThrowExceptions

Exception or error:

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?

How to solve:

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" />

###

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);
        }
    }
});

###

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

Leave a Reply

Your email address will not be published. Required fields are marked *