朋友我正在制作一个安全应用程序,当用户单击忘记密码时,我想将他的密码发送到与 Google play 帐户关联的他的 G-mail 帐户。所以问题是,我怎样才能知道他的 G-mail ID?
问问题
1192 次
2 回答
5
您可以使用 AccountManager.getAccounts 或 AccountManager.getAccountsByType 来获取设备上所有帐户名称的列表。
Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
Account[] accounts = AccountManager.get(context).getAccounts();
for (Account account : accounts) {
if (emailPattern.matcher(account.name).matches()) {
String possibleEmail = account.name;
...
}
}
需要 GET_ACCOUNTS 权限:
使用权限 android:name="android.permission.GET_ACCOUNTS" />
于 2012-11-20T08:13:58.963 回答
1
这是获取与 Google Play 关联的 G-mail 帐户的代码片段
private String getEmailID() {
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;
break;
}
}
return gmail;
}
还要在 AndroidManifest.xml 中添加此权限
<uses-permission android:name="android.permission.GET_ACCOUNTS" >
于 2012-11-20T08:16:23.287 回答