我見過使用AccountManager類似Accessing Google Account Id /username via Android的引用,但它好像是用來抓取authtoken的?如何在Android上獲取Google用戶名?
我只需要訪問用戶名,密碼或任何驗證令牌。
我正在使用android 2.1 sdk。
我見過使用AccountManager類似Accessing Google Account Id /username via Android的引用,但它好像是用來抓取authtoken的?如何在Android上獲取Google用戶名?
我只需要訪問用戶名,密碼或任何驗證令牌。
我正在使用android 2.1 sdk。
正如評論中提到的,羅曼對How to get the Android device's primary e-mail address的回答解決了這個問題。 這裏是我使用的代碼,也會從電子郵件中刪除用戶名。
public String getUsername() {
AccountManager manager = AccountManager.get(this);
Account[] accounts = manager.getAccountsByType("com.google");
List<String> possibleEmails = new LinkedList<String>();
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.
possibleEmails.add(account.name);
}
if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
String email = possibleEmails.get(0);
String[] parts = email.split("@");
if (parts.length > 1)
return parts[0];
}
return null;
}
只是想指出的是,如果用戶擁有多個賬戶,這將無法正常工作,而且很可能將無法正常工作,如果他們不要不使用GMail。我有多達3個Google帳戶,我的個人帳戶,一個用於工作的帳戶,以及一個當我激活我的手機時強制我註冊的Gmail,但後來我通過桌面瀏覽器刪除了這些帳戶。 – Chloe 2012-04-07 19:45:14
@Chloe如何選擇一個Google帳戶並保存? – Zyoo 2013-03-15 11:12:27
我可以使用oauth2發送郵件,有時它不工作..如何得到一個確認郵件發送成功使用這個令牌與Gmail ..因爲如果不是郵件我有一個選項發送短信 – 2013-05-17 06:27:44
看到我的迴應:http://stackoverflow.com/questions/2112965 – 2010-04-28 07:24:10