How do I retrieve the logged in Google account on android phones?
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)
How to get android phone user's google account name?
try this
AccountManager manager = AccountManager.get(context);
Account[] accounts = manager.getAccountsByType("com.google");
List<String> username = new LinkedList<String>();
for (Account account : accounts) {
username.add(account.name);
}
and add permission to android manifest
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
How to find Gmail account associated with Android Market?
The dialog
is caused by the implementation of AbstractAccountAuthenticator#getAccountRemovalAllowed in the Google account authenticator.
Since calling implementations of AbstractAccountAuthenticator
directly is prevented by the system-only permission android.permission.ACCOUNT_MANAGER
you're going to have a hard time finding this out on your own.
Related Topics
How to Implement Drag and Drop in Android 2.2
How to Create Colorstatelist Programmatically
Returning from an Activity Using Navigateupfromsametask()
How Is Staticlayout Used in Android
Android - Switch Actionbar Back Button to Navigation Button
Make an Android Button Change Background on Click Through Xml
Android O - Old Start Foreground Service Still Working
Ongloballayoutlistener: Deprecation and Compatibility
Progressdialog Not Shown When Asynctask.Get() Called
How to Parse Same Name Tag in Android Xml Dom Parsing
Refresh Fragment UI from Fragmentactivity
Difference Between Android-Support-V7-Appcompat and Android-Support-V4
Keep a Service Running Even When Phone Is Asleep
Error: Resource Android:Attr/Fontvariationsettings Not Found
Android - Get Time of Chronometer Widget
Open Soft Keyboard Programmatically