How can I get the google username on Android?
Asked Answered
D

2

19

I've seen references to using the AccountManager like Accessing Google Account Id /username via Android. But it seems like it's for grabbing the authtoken?

I just need access to the username, no passwords or any auth tokens.

I'm using android 2.1 sdk. How can I get the google username on Android?

Darken answered 28/4, 2010 at 5:28 Comment(1)
See my response here: stackoverflow.com/questions/2112965Bolivar
D
46

As mentioned in the comments, Roman's answer to How to get the Android device's primary e-mail address solves it. Here's the code i used that will also strip out the username from the email.

public String getUsername() {
    AccountManager manager = AccountManager.get(this); 
    Account[] accounts = manager.getAccountsByType("com.google"); 
    List<String> possibleEmails = new LinkedList<String>();

    for (Account account : accounts) {
      // TODO: Check possibleEmail against an email regex or treat
      // account.name as an email address only for certain account.type values.
      possibleEmails.add(account.name);
    }

    if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
        String email = possibleEmails.get(0);
        String[] parts = email.split("@");

        if (parts.length > 1)
            return parts[0];
    }
    return null;
}
Darken answered 30/4, 2010 at 0:51 Comment(4)
Just want to point out that this won't work if the user has multiple accounts, and probably won't work if they don't use GMail. I have had up to 3 Google accounts, my personal, one for work, and one GMail that Google forced me to sign up for when I activated my phone, but which I subsequently deleted through my desktop browser.Kourtneykovac
@Kourtneykovac How to select one of the Google accounts and save it?Ratiocination
I can able to send mail using oauth2 and sometimes its not working.. how to get an acknowledge that mail is send successfully using this token with Gmail..because if not mail I have an option to send smsValley
This should always have at least one account so that all the Google apps will work. Its possible that it doesn't, but it will be an edge case for an Android device. Note however that it may not be a gmail address for a number of reasons.Piperonal
C
0

In new Android version you can't get the accounts with code due to security reason:-

it needs to be prompt to user, and if user agreed then only can be proceed with it.The code will be looks like below :-

 private val chooseAccount = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){ result: ActivityResult ->
    result.apply {
        if (resultCode == RESULT_OK) {
            Timber.d("resultCode ==RESULT_OK")
            Timber.d(data?.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE))
            Timber.d(data?.getStringExtra(AccountManager.KEY_ACCOUNT_NAME))
        } else if (resultCode == RESULT_CANCELED) {
            Timber.d("resultCode ==RESULT_CANCELED")
        }
    }
   
}




 override fun onCreate(savedInstanceState: Bundle?) {
//Note: setAllowableAccountsTypes you can change it as per your need
    chooseAccount.launch(
            AccountPicker.newChooseAccountIntent(
                AccountPicker.AccountChooserOptions.Builder()
                    .setAllowableAccountsTypes(listOf(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE))
                    .setAlwaysShowAccountPicker(true)
                    .build()
            ))
}
Camphene answered 27/3, 2022 at 10:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.