【问题标题】:How to get Current Active Google play account in Android如何在 Android 中获取当前活跃的 Google Play 帐户
【发布时间】:2016-03-10 10:15:53
【问题描述】:

我可以使用下面的代码 sn-p 获取所有配置的 gmails

 Account[] accounts = AccountManager.get(this).getAccounts();

for (Account account : accounts) {

 Log.e("Gmail","gmail "+account.name);

}

通过上述 sn-p,我收到了不止一个 Gmail。但我只需要当前活跃的 gmail。指导我解决这个问题。

【问题讨论】:

    标签: android google-play


    【解决方案1】:

    There is no concept of primary email id or current active google account in android.

    您可以获取已登录该手机的用户的谷歌帐户。

    Account[] accounts = AccountManager.get(this).getAccounts();
    if(accounts != null && accounts.length > 0) {
        ArrayList playAccounts = new ArrayList();
        for (Account account : accounts) {
            String name = account.name;
            String type = account.type;
            if(account.type.equals("com.google")) {
                playAccounts.add(ac.name);
            }
            Log.d(TAG, "Account Info: " + name + ":" + type);
        }
        Log.d("tag", "Google Play Accounts present on phone are :: " + playAccounts);
    }
    

    根据上面链接中建议的答案是选择您在列表中获得的最后一个帐户,以获取在 android 中添加的第一个帐户。

    String emailId = playAccounts.get(playAccounts.size()-1);
    

    更新 - 1

    您需要GET_ACCOUNTS 权限才能访问帐户。

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

    希望对你有所帮助。

    【讨论】:

    • 我更改了解决方案并添加了有关主电子邮件 ID 的说明。
    猜你喜欢
    • 2016-01-25
    • 2021-07-16
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 2023-03-16
    • 2010-09-20
    • 1970-01-01
    相关资源
    最近更新 更多