androidandroid-6.0-marshmallowandroid-account

Getting the Gmail Id of the User In Android 6.0 marshmallow


I am getting email id by using android.permission.GET_ACCOUNTS permission.

 try {
            Account[] accounts = AccountManager.get(this).getAccountsByType("com.google");
            for (Account account : accounts) {
                emailid = account.name;
                Log.e("account",emailid);
            }
        } catch (Exception e) {
            Log.i("Exception", "Exception:" + e);
        }

This code is working on all devices upto Lollipop 5.1. But its not working in Marshmallow 6.0.

Can anyone help me to resolve this problem. I am not even getting any errors in logcat.


Solution

  • This code is working, Tested on Android 4.4.4, 5.0.1, 6.0 and 6.0.1

    String possibleEmail = "";
        final Account[] accounts = AccountManager.get(context).getAccounts();
        //Log.e("accounts","->"+accounts.length);
        for (Account account : accounts) {
            if (Patterns.EMAIL_ADDRESS.matcher(account.name).matches()) {
                possibleEmail = account.name;
            }
        }
    

    possibleEmail is the email of the device.