Search code examples
androidemailgmailuser-accounts

How to get the device email in Android?


I am using Android 6.0 and I am trying to get the device's email(s) which are logged in to that particular device.

I have already followed many answer in Stack Overflow like:

How to get the Android device's primary e-mail address

How can you get an Android user's email address?

I tried to get the email array by doing this:

Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
        Account[] accounts = AccountManager.get(this).getAccounts();
        Log.i("email","email : "+accounts.length);

But I have never been lucky to get any email in that array. And the Log I have mentioned always print 0 as the size.

Also I have added the following permission in AndroidMainfest.xml:

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

What is it that I am doing wrong?


Solution

  • Not all Android devices have to have email addresses saved. Especially, the rooted ones. Do not rely on this solutions. If you really need the email address of user, ask him/her to type.