This question一般会讨论Android设备,但是如果你试图在Kindle Fire上运行这个代码,你得到的只是用户的名字。有没有办法获得电子邮件地址?我们希望弹出一个已预先填充的电子邮件地址的对话框,这样如果它是正确的,他们就不必输入它,但似乎唯一的解决办法就是让它们重新输入它。
编辑:这是其他线程建议的代码(在Kindle Fire上不起作用):
Account[] accounts = AccountManager.get(this).getAccounts();
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.
String possibleEmail = account.name;
// possibleEmail is a list of account names, hopefully including the @gmail.com address.
}
答案 0 :(得分:0)
在Kindle Fire上,是否可以获取用户的电子邮件地址?
抱歉,但你完全错了。
我用于链接到Google登录对话框,其中显示了Kindle Fire上存在的所有用户。
请遵循以下代码:
public class AuthAcount {
private Context context;
private AccountManager mAccountManager;
public AuthAcount(Context context) {
setContext(context);
}
public Account[] getAccount() {
mAccountManager = AccountManager.get(context);
Account[] accounts = mAccountManager
.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
return accounts;
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
}
P / s:要使用GoogleAuthUtil类,可能需要google-oauth-client-1.15.0-rc.jar(请使用最新版本)。
答案 1 :(得分:0)
在我可以使用的Fire设备上,这就是我所做的:
final static String TYPE_GOOGLE = "com.google";
final static String TYPE_AMAZON = "com.amazon";
final static String AMAZON_EMAIL = "com.amazon.pim";
public static String getAccountName(final Context context) {
boolean amazon = TextUtils.equals(Build.MANUFACTURER, "Amazon");
String type = amazon ? TYPE_AMAZON : TYPE_GOOGLE;
String email = null;
try {
Account acc[] = AccountManager.get(context).getAccountsByType(type);
if (acc.length > 0) {
email = acc[0].name;
for (int i = 0; i < acc.length; i++) {
Account account = acc[i];
if (amazon) {
// there are a lot of flags, just skip them
if (!TextUtils.equals(account.type, TYPE_AMAZON)) {
// is it an email account?
if (account.type.startsWith(AMAZON_EMAIL)) {
email = account.name;
}
}
}
}
} else {
acc = AccountManager.get(context).getAccounts();
if (acc.length > 0) {
// just return the first one...
email = acc[0].name;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return email;
}
这也适用于Google Play设备。