【问题标题】:Retrieve Google user name from device从设备中检索 Google 用户名
【发布时间】:2015-04-24 19:44:46
【问题描述】:

我必须在不使用 Google API 的情况下从设备中检索 Google 用户名。我尝试了一些堆栈溢出代码,但没有成功。 请帮忙

【问题讨论】:

  • 您是说 Google 帐户电子邮件?

标签: android


【解决方案1】:

试试这个

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;
        Toast.makeText(getApplicationContext(), gmail, Toast.LENGTH_LONG).show();
        break;
    }
}

一切顺利

【讨论】:

    【解决方案2】:

    使用以下代码:

    public class UserEmailFetcher {
    
        static String getEmail(Context context) {
            AccountManager accountManager = AccountManager.get(context);
            Account account = getAccount(accountManager);
    
            if (account == null) {
                return null;
            } else {
                return account.name;
            }
        }
    
        private static Account getAccount(AccountManager accountManager) {
            Account[] accounts = accountManager.getAccountsByType("com.google");
            Account account;
            if (accounts.length > 0) {
                account = accounts[0];
            } else {
                account = null;
            }
            return account;
        }
    
    }
    

    添加一个权限:

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

    参考答案:this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多