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;
}
}
Related
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
I want to get the email id from the Android phone having oreo. For below 8, I am able to get the email by using below code:
String emailId = "null";
// get the accounts info
AccountManager accountManager = AccountManager.get(context);
Account[] accounts = accountManager.getAccountsByType("com.google");
// if the accounts are not null
if (accounts.length > 0 && accounts[0] != null) {
emailId = accounts[0].name;
}
return emailId;
I am also getting the permission below: Runtime too. It is working in 7.0 too.
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
Need to get the email id from the user account so to automatically display it on the screen while he wants to contact me through app!!
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 working on registration screen on my app. I need to get the email suggestion while typing. It doesn't need to sync with Gmail Id accounts. It should get the email id which I am using as account which we can see in account settings.
User should have Gmail account in his android phone
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
/**
* This class uses the AccountManager to get the primary email address of the
* current user.
*/
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;
}
}
In Manifest
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
Source: https://stackoverflow.com/a/2556540/950427
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;
}
}