如何在Android中获取当前有效的Google Play帐户 [英] How to get Current Active Google play account in Android
问题描述
我可以使用以下代码段获取所有已配置的gmails
I can able to get all the configured gmails using below code snippet
Account[] accounts = AccountManager.get(this).getAccounts();
for (Account account : accounts) {
Log.e("Gmail","gmail "+account.name);
}
使用上面的代码片段,我得到了不止一个Gmail.但我只需要当前的活动gmail.指导我解决这个问题.
With the above snippet, I am getting more than one Gmail. But I need only current active gmail. Guide me to solve this.
推荐答案
There is no concept of primary email id or current active google account in android.
您可以获取已在该电话上登录的用户的Google帐户.
You can get the google accounts of user which has been logged in on that phone.
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中添加的第一个帐户.
As per the answer suggested in above link is to select the last account which you got in list, to get the first account added in android.
String emailId = playAccounts.get(playAccounts.size()-1);
更新-1
您需要具有GET_ACCOUNTS
权限才能访问帐户.
You'll need GET_ACCOUNTS
permission to access accounts.
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
希望会有所帮助.
这篇关于如何在Android中获取当前有效的Google Play帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!