Reputation: 3488
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?
Upvotes: 0
Views: 361
Reputation: 6350
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;
}
}
Upvotes: 4