Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve the logged in Google account on android phones?

Tags:

I am developing an Android application and I need to retrieve the Google account used on the phone. I want to do this for the C2DM, but I don't want to ask the user to enter in his/her Google email account if they are already logged in. Is there any way to do it?

like image 841
fanar Avatar asked Oct 27 '10 22:10

fanar


People also ask

Where is my phone signed into Google?

Go to your Google Account. On the left navigation panel, select Security . On the Your devices panel, select Manage all devices. You'll see devices where you're currently signed in to your Google Account or have been in the last few weeks.

How do I see previously logged in to Gmail?

You can check your Gmail login history using a Google feature called Last account activity. Go to your Gmail account and look down in the right-hand bottom corner. There, you'll see a link called Details. Click it to be shown a list of all recent logins, along with their corresponding IP addresses.

Where are Google accounts stored in Android?

The authentication token for Google accounts and of other accounts which uses AccountManager class are stored inside: /data/system/users/0/accounts. db # for Android Marshmallow and earlier /data/system_ce/0/accounts_ce.


1 Answers

Something like this should work:

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;     } } 

And you will need the following permission in your manifest:

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

Remember to 'Requesting Permissions at Run Time' if you support Android 6 and later https://developer.android.com/training/permissions/requesting.html

I wrote this from memory so it may need a little tweaking. Apparently it's possible to register now without an email address, so maybe do some regexing on the data to ensure it's actually an email address (ensure it contains @gmail or @googlemail)

like image 162
Teario Avatar answered Oct 27 '22 00:10

Teario