如何在 Android 上获取 google 用户名?

发布于 2024-08-30 17:38:14 字数 309 浏览 11 评论 0原文

我见过有关使用 AccountManager 的参考,例如访问通过 Android 的 Google 帐户 ID /用户名。不过好像是为了抢authtoken

我只需要访问用户名,不需要密码或任何身份验证令牌。

我使用的是android 2.1 sdk。如何在 Android 上获取 google 用户名?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜警司 2024-09-06 17:38:14

正如评论中提到的,Roman 对 如何获取 Android 设备的主电子邮件地址 的回答解决了这个问题。
这是我使用的代码,它也会从电子邮件中删除用户名。

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

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;
}
一片旧的回忆 2024-09-06 17:38:14

在新的 Android 版本中,由于安全原因,您无法使用代码获取帐户:-

需要提示用户,如果用户同意,则只能继续操作。代码将如下所示:-

 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()
            ))
}

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()
            ))
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文