Monday, May 14, 2012

How to retrieve all the accounts in Android Device?


With the help of Account Manager we can retrieve the list of all account in android device..


public String getUsername() {
AccountManager manager = AccountManager.get(this);
Account[] accounts = manager.getAccountsByType("com.google");
List<String> possibleEmails = new LinkedList<String>();


for (Account account : accounts) {
/*We can fetch account name using:
account.name;
and the encrypted password using:
manager.getPassword(account);*/
possibleEmails.add(account.name);

}


if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
String email = possibleEmails.get(0);
String[] parts = email.split("@");
if (parts.length > 0 && parts[0] != null)
return parts[0];
else
return null;
} else
return null;
}

No comments:

Post a Comment