0

Can you Help me with this Code? It should get emailadresses from the current Android Device but it Returns null every time. I have tried it with this but still getting null every time

 public String getUsername() {
    Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
    Account[] accounts = AccountManager.get(getApplicationContext()).getAccounts();
    for (Account account : accounts) {
        if (emailPattern.matcher(account.name).matches()) {
            String possibleEmail = account.name;
            return possibleEmail;
        }
    }
    return null;
}

Here my Manifest File:

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="Upload FTP"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user12346352
  • 176
  • 2
  • 20

2 Answers2

2

The below code works great for me. All you have to do is

In your OnCreate() have the below code and tweak as per your requirement

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()) {
                accountsList.add(account.name);
                Toast.makeText(getApplicationContext(), account.name, Toast.LENGTH_SHORT).show();
            }
        }

and then you have to add Permission of GET_ACCOUNTS in AndroidManifest.xml file

 <uses-permission android:name="android.permission.GET_ACCOUNTS" />
Abb
  • 3,051
  • 3
  • 17
  • 34
0

There is no requirement for the user's email address to be visible to other apps by any means. There certainly is no requirement that the user have an account in AccountManager whose account name is an email address and is an email address that the user actively uses. For example, just because the user set up a Google account on the device does not mean that the user uses the Gmail associated with that account.

If you want the user's email address, ask them what they want to use.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • If i understand that Right a Windows should Pop up where he can enter the emailadress? Can you give me a Code example how I can do that? – user12346352 Jul 04 '18 at 12:34