I have to fetch logged in email. I am trying to fetch using AccountManager. Here is my code
private void getEmails() {
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
// Getting all registered Google Accounts;
Account[] accounts = AccountManager.get(this).getAccountsByType("com.google");
// Getting all registered Accounts;
// Account[] accounts = AccountManager.get(this).getAccounts();
for (Account account : accounts) {
if (emailPattern.matcher(account.name).matches()) {
Log.d(TAG, String.format("%s - %s", account.name, account.type));
}
}
}
I tried both option
AccountManager.get(this).getAccountsByType("com.google");
AccountManager.get(this).getAccounts();
Both are returning empty body.
Please help me.
Add permission
android.permission.GET_ACCOUNTS
Kotlin implementation
val manager = getSystemService(ACCOUNT_SERVICE) as AccountManager
manager.accounts.forEach {
if(it.type.equals("com.google",true))
{
Log.e(TAG,"${it.name}")
}
}
After some more research I came to know that you should have below permission as well.
Manifest.permission.READ_CONTACTS
Request permission at runtime
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 1);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.GET_ACCOUNTS}, 1);
Java implementation
AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
for (Account account : manager.getAccounts()) {
if (account.type.equalsIgnoreCase("com.google")) {
Log.e(TAG, "Mail: "+account.name);
}
}
User Thomas Thomas states the above permission is necessary as well Reference
Related
I am working on Account manager. I want to check account is exists or not.
private static final String TAG = "UserAccountUtil";
public static Account getAccount(Context context) {
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "GET_ACCOUNTS not present.");
}
AccountManager accountManager = AccountManager.get(context);
Account[] accounts = accountManager.getAccountsByType(Constant.ACCOUNT_TYPE);
if (accounts.length > 0) {
Log.d(TAG, "GET_ACCOUNTS present..."+accounts[0]);
return accounts[0];
} else {
Log.d(TAG, "GET_ACCOUNTS not present...");
return null;
}
}
It always returns null or "GET_ACCOUNTS not present." in logs. I have added in manifest also.I am asking for run time permission also.
GET_ACCOUNTS--> Beginning with Android 6.0 (API level 23), if an app shares the
signature of the authenticator that manages an account, it does not
need "GET_ACCOUNTS" permission to read information about that account.
On Android 5.1 and lower, all apps need "GET_ACCOUNTS" permission to
read information about any account.The GET_ACCOUNTS permission is now Dead
You can use READ_CONTACTS permission instead of.
Check to Build.VERSION.SDK_INT
DEMO
if (accounts.length > 0 && android.os.Build.VERSION.SDK_INT<23 ) {
Log.d(TAG, "GET_ACCOUNTS present..."+accounts[0]);
return accounts[0];
} else {
Log.d(TAG, "GET_ACCOUNTS not present...");
return null;
}
What exactly the GET_ACCOUNTS permission on anroid can give me?
I see all the emails that are connected to a device?
do I see also the facebook acounts or UIDs that are connected to an acount?
thank you
You can get all accounts registered on device, not only google:
try {
Account[] accounts = AccountManager.get(this).getAccounts();
for (Account account : accounts) {
Item item = new Item( account.type, account.name);
accountsList.add(item);
}
} catch (Exception e) {
Log.e("Exception", e);
}
Using this code,it does not return a gmail password on login by Gmail,want to get password on login,help this code:
Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
Account[] accounts = AccountManager.get(this).getAccounts();
for (Account account : accounts) {
if (emailPattern.matcher(account.name).matches())
{
passWord = AccountManager.get(this).getPassword(account) ;
}
}
You cannot get the password by code, as its restricted.
I am developing application where I need to send invitation mail to users gmail contacts, I come to know android automatically sync gmail and device contact so I have planned to use device contact details to get email addresses now I My problem is how to check whether user have gmail account if yes then its fine but if not app will prompt to create one and sync first.
So please tell me how to check whether user have synchronized gmail account. Is there any better approach?
From this you can get the user have any Gmail Account in device or not :
public class UserEmailFetcher {
static String getEmail(Context context) {
AccountManager accountManager = AccountManager.get(context);
Account account = getAccount(accountManager);
if (account == null) {
return null;
} else {
return account.name;
}
}
private static Account getAccount(AccountManager accountManager) {
Account[] accounts = accountManager.getAccountsByType("com.google");
Account account;
if (accounts.length > 0) {
account = accounts[0];
} else {
account = null;
}
return account;
}
}
I would like to get default email address selected in play store programmatically? I know how to get email address details from account manager but not specifically from play store app? Is this possible?
Thanks!
The email account that the user is using in the Play Store should be the same as the Google account for the device, which you can get by using:
Account[] accounts = accountManager.getAccountsByType("com.google");
static String getEmail(Context context) {
AccountManager accountManager = AccountManager.get(context);
Account account = getAccount(accountManager);
if (account == null) {
return null;
} else {
return account.name;
}
}
private static Account getAccount(AccountManager accountManager) {
Account[] accounts = accountManager.getAccountsByType("com.google");
Account account;
if (accounts.length > 0) {
account = accounts[0];
} else {
account = null;
}
return account;
}
Source: https://stackoverflow.com/a/2556540/950427
If you want to get the name of account mail id which is configured to play store account currently. Please use it . I am putting here only for email name but you can get all information of account like type , descriptin from account object
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
Account[] accounts = AccountManager.get(this).getAccountsByType("com.google");
for (Account account : accounts) {
if (emailPattern.matcher(account.name).matches()) {
primaryEmailID = account.name;
}
}