0

Is there any good react native library to get primary email address of android devices. There is a Stackoverflow question to get it with native android (link). I need include it in react native android app.

Update: I could get email address from following method with android native. Now I need to implement it in react native.

permission:

uses-permission android:name="android.permission.GET_ACCOUNTS"

method:

  String emailAddress = "";
    private String getEmail() {
        int i = 0;
        try {
            Account[] accounts = AccountManager.get(this).getAccountsByType(
                    "com.google");
            for (Account account : accounts) {
                if (i > 0)
                    sGoogleId = account.name;
                else
                    sGoogleId = account.name;
                i++;

            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return emailAddress;
    }
6
  • 1
    That question's accepted answer is incorrect, insofar as it is not guaranteed to get you any valid email address. Commented Mar 18, 2017 at 14:37
  • @CommonsWare Atleast I need to get a list of all account names on the device. In our case we can have a rule to restrict it to a single account. Is it possible?
    – LittleOne
    Commented Mar 18, 2017 at 14:41
  • I do not know whether React Native has any support for this. My point is that you cannot get a useful user's email address this way. If you want to get the email address that the user wants to use, ask them for their email address. Commented Mar 18, 2017 at 15:00
  • @CommonsWare we can't do that. We need to restrict users based on email domain address.
    – LittleOne
    Commented Mar 18, 2017 at 15:29
  • That is impractical. There are many email apps and users that do not use the Android account system. You have no means of accessing those email addresses programmatically. Conversely, there are lots of people who might have a Google account registered, but they never use any associated Gmail account. Or, the Gmail account is not one that the user wants you to use (e.g., it is a work account). Commented Mar 18, 2017 at 15:57

1 Answer 1

0

I could solve this by using react native Native module. Device should have at-least one google account. Since it takes some time to process the request need to collect with a promise (androidManager.getAccounts().then(){}.Done(){}).

11
  • 1
    I fail to see how this is the answer?
    – Norfeldt
    Commented Mar 9, 2018 at 22:18
  • @Norfeldt Is there any wrong with above answer? It sorted my problem. What is the problem you have?
    – LittleOne
    Commented Mar 11, 2018 at 14:57
  • Trying import { androidManager } from 'react-native' is not possible. I can't find any docs on androidManager on the offical react native site
    – Norfeldt
    Commented Mar 11, 2018 at 14:59
  • @Norfeldt You can't use it like that. Above code is native android code. But you can use native codes in a react native apps (react native Native module).
    – LittleOne
    Commented Mar 11, 2018 at 15:30
  • Check following link. You can use that library. npmjs.com/package/react-native-account-manager
    – LittleOne
    Commented Mar 11, 2018 at 15:32

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.