14

I am working on a project, and I have to fill the EditText automatically with the user's primary email, I am considering primary email as the email that associated with google play store. I have read this post and implemented that.

If we are using the AccountManager class for geting the email ids, we will get all the email id added in that phone, so that is not possible, and some says to take the first email id that returned by the AccountManager, but that returns the email id that added in the phone for the first time.

ie, suppose I have added test@gmail.com and linked that with Google Play, later I have added test_new@gmail.com and associated this account with play store, right now I am using Play store with this account. If I have wrote code as follows:

    Account[] accountList = AccountManager.get(this).getAccountsByType("com.google");
Log.d("Play store account:" , accountList[0].name);

the expected out put for the statement is test_new@gmail.com, but I am getting test@gmail.com

How can I solve this issue?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
droidev
  • 7,352
  • 11
  • 62
  • 94
  • 1
    expected out put for the statement is test@gmail.com, but I am getting test@gmail.com ? – N J Sep 08 '15 at 06:53
  • typo, expected output is the email id that associated with play store, here it is test_new@gmail.com – droidev Sep 08 '15 at 06:55
  • 1
    You can try this..it has been answered already [here][1] [1]: http://stackoverflow.com/questions/2112965/how-to-get-the-android-devices-primary-e-mail-address – Thiyagu Sep 08 '15 at 07:01
  • please read the question, I have mentioned that link in question itself, answer in that post is not relevant for me, because it is returns the account list only – droidev Sep 08 '15 at 07:02
  • I don't think it's possible to know which google (gmail) account was used for the Play Store. – Jared Rummler Mar 02 '16 at 06:51
  • How about [this one](http://stackoverflow.com/a/2556540/1893766)? The comment in the code says _This class uses the AccountManager to get the **primary email address** of the current user_ – ozbek Mar 03 '16 at 03:44

5 Answers5

10

As far as I read, there is no concept of primary email id in android. and there is no way to get e-mail id associated with play store. so what I did is, I have fetched all gmail ids and took the last one, it is not the main email id, but it should be the first added google account in his device. so in normal use cases user won't play with his first added email id. so we can assume it as primary mail id.

droidev
  • 7,352
  • 11
  • 62
  • 94
3

I agree with @driodev, but I have done it with a different approach. Just copy and paste the code... I might be answering this question a bit late but I guarantee this will be helpful to many in future. In fact, I think we can get any account id that is used in the android device just by changing the string in getAccount("com.example"). Here is the code.

String userEmailId = getEmailID(getApplicationContext());

 private String getEmailID(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;
}
Amit Hooda
  • 2,133
  • 3
  • 23
  • 37
  • I tried this, but why is it that I keep getting null for my Android device when I print it? My device is running only on one Google account, gmail. – DaveNOTDavid Apr 20 '16 at 17:37
  • did you add permission in manifest !! did you check for typo mistake say for ex in the above "com.google" i was having it as "com.gogle" for a wile i could not figure out where the problem was.. i would be helpful if you could provide your code snippet – Mohammed Nathar Apr 21 '16 at 04:56
  • this should be the permission in the manifest – Mohammed Nathar Apr 21 '16 at 05:07
  • Yup, I already included the permission into my manifest. Here's my code snippet: http://stackoverflow.com/questions/36750402/why-do-i-get-null-from-retrieving-the-users-gmail – DaveNOTDavid Apr 22 '16 at 04:15
  • Mine's 6.0, and hers is 4.4... Could that be the reason why? – DaveNOTDavid Apr 22 '16 at 13:34
  • i assume that this code works fine on version 4.4 and not on 6.... i think google has made changes in account manager .. now this makes me check my app again for compatibility purpose... please vote my answer if you found it usefull – Mohammed Nathar Apr 22 '16 at 14:42
  • dpark@ did you figure out why you were getting null for email for your android device (version 6.0) – Mohammed Nathar May 04 '16 at 10:04
  • 1
    Me too have this problem. This need to Read Contacts Permission also. – Vishnu V Jun 10 '16 at 07:27
2

The account you have "associated with the Play Store" is just an app preference of the Play app. You can't read that. The user could download/purchase some apps with test and some with test_new.

bryan
  • 798
  • 7
  • 18
1

Some additions to @MohammedNathar's answer. On Android 6.0 and above, don't forget to request permissions, because this versions are some paranoic:

Manifest:

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

Request:

private void requestPermissions(Activity activity) {
    if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.GET_ACCOUNTS}, 101);
        return null;
    }
}

And in activity

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    boolean result = false;
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
        result = true;
    switch (requestCode) {
        case 101:
            if (result) Log.d(Constants.LOG, "Permission GET_ACCOUNTS granted");
            break;
    }
}

Or you can do it with one "if" if you want.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
1

Whenever user assign email_id then android create a calendar with Email.So if above solution not turning out for you then you can try this as hack. Find Email associated with Calendar.

    public String getCalendarIdAndEmail(Context c) {

    String projection[] = {"_id", "calendar_displayName"};
    //   Uri calendars = Uri.parse("content://com.android.calendar/calendars");
    String calID = null;
    try {
        ContentResolver contentResolver = c.getContentResolver();

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
            return calID;
        }
        Cursor managedCursor = contentResolver.query(CalendarContract.Calendars.CONTENT_URI, projection, CalendarContract.Calendars.VISIBLE + " = 1 AND " + CalendarContract.Calendars.IS_PRIMARY + "=1", null, CalendarContract.Calendars._ID + " ASC");
        if (managedCursor.getCount() <= 0) {
            managedCursor = contentResolver.query(CalendarContract.Calendars.CONTENT_URI, projection, CalendarContract.Calendars.VISIBLE + " = 1", null, CalendarContract.Calendars._ID + " ASC");
        } else {
            Log.d("getCount", "" + managedCursor.getCount());
        }

        if (managedCursor.moveToFirst()) {

            int nameCol = managedCursor.getColumnIndex(projection[1]);
            int idCol = managedCursor.getColumnIndex(projection[0]);
            do {
               String calName = managedCursor.getString(nameCol);
                calID = managedCursor.getString(idCol);
                //CalName is Email id you are looking for
                Log.e("tag", "calName " + calName + "____calId " + calID);

            } while (managedCursor.hasNext());//managedCursor.moveToNext());
            managedCursor.close();
        }
    } catch (Exception e) {
        Log.e("error", e.getMessage(););
    }
    return calID;
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Prinkal Kumar
  • 3,196
  • 2
  • 10
  • 15