AccountsManager.get(this).getAccounts is Always 0 - android

I am targeting my app to API 27 and calling getAccounts on Account manager and still I get 0 Result.
I have asked for permissions
android.permission.GET_ACCOUNTS
android.permission.AUTHENTICATE_ACCOUNTS
Account[] accounts = AccountManager.get(context).getAccounts();
if(accounts.length == 0){
return null;
}
accounts is always 0

Related

Android get logged in email name programmatically. Android AccountManager not working

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

GET_ACCOUNT permission is not granted in android N

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;
}

How to get google logged in email id for Android Device having OREO (Android 8)

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!!

How to check whether user have gmail account

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;
}
}

how to sync phone contacts into gmail in android programmatically using google contacts api

How to synchronize Android native contacts into Google account using Google API.
Provide some useful links.
The syncing happens automatically. You can add or delete contacts programatically. But the syncing is handled by the OS automatically if and only if the user has enabled 'sync conatcts' option in phone settings.
You can, however run a sync routine that can call the syncing process if syncing is enabled by the user using something like this:
private void requestSync()
{
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccounts();
for (Account account : accounts)
{
int isSyncable = ContentResolver.getIsSyncable(account, ContactsContract.AUTHORITY);
if (isSyncable > 0)
{
Bundle extras = new Bundle();
extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
ContentResolver.requestSync(accounts[0], ContactsContract.AUTHORITY, extras);
}
}
}
The following could also be a good answer. Its similar to the above one, but default Settings app uses code something like this:
private void requestSyncForAccounts() {
SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypes();
Bundle extras = new Bundle();
extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
Account[] accounts = AccountManager.get(PeopleActivity.this).getAccounts();
for (Account account : accounts) {
for (int j = 0; j < syncAdapters.length; j++) {
SyncAdapterType sa = syncAdapters[j];
if (ContentResolver.getSyncAutomatically(account, sa.authority)) {
ContentResolver.requestSync(account, sa.authority, extras);
}
}
}
}

Categories

Resources