0

Is it possible to fetch the details without the user actually signing in? Can we directly fetch the details from the google account on the phone?

I am trying to create an app with drawer functionality and want to display the logged in google user's profile picture, name and email (like seen on playstore etc.)

The solution on the similar question doesn't work for me. - I get an empty list using @shylendra's answer on android 8.0 and 8.1, didnt test it below that.

Harshad Loya
  • 123
  • 1
  • 2
  • 13

2 Answers2

0

Is it possible to fetch the details without the user actually signing in?

Yes, You can by using AccountManager as answered here

AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] list = manager.getAccounts();
String gmail = null;

for(Account account: list)
{
    if(account.type.equalsIgnoreCase("com.google"))
    {
        gmail = account.name;
        break;
    }
}
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
  • I tried it, but I get an empty list even after giving permission. – Harshad Loya Feb 19 '18 at 16:19
  • @HarshadLoya Are you using real device or emulator? Did you sign in with Google account on mobile? – Shailendra Madda Feb 20 '18 at 04:40
  • @shylendra - Tried both, and yes I am signed in with google account on my phone, infact there are over 10 accounts I am signed in into other than google account. Refer this question, i have added code as well and found out a little more : https://stackoverflow.com/q/48875946/5093418 – Harshad Loya Feb 20 '18 at 15:04
0

Try this!!!

AccountManager helps to get the details of an account associated with the phone

AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] list = manager.getAccounts();

Add these permissions.

<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
Arnold Brown
  • 1,330
  • 13
  • 28