Vous pouvez utiliser le service AccountManager
pour que:
Account[] accounts = AccountManager.get(this).getAccounts();
for (Account account : accounts) {
Log.d(TAG, account.type + "/" + account.name);
}
Remarque, nécessite l'autorisation GET_ACCOUNTS
, si vous ciblez Android M et au-dessus, vous aurez besoin ALSE de demander à l'utilisateur de cette autorisation par le modèle Runtime Permissions
.
MISE À JOUR
Pour sauter tous les comptes qui ne supportent pas les contacts du tout, ou ne les contacts de support, mais sont en lecture seule (comme Viber et WhatsApp):
final SyncAdapterType[] syncs = ContentResolver.getSyncAdapterTypes();
for (SyncAdapterType sync : syncs) {
Log.d(TAG, "found SyncAdapter: " + sync.accountType);
if (ContactsContract.AUTHORITY.equals(sync.authority)) {
Log.d(TAG, "found SyncAdapter that supports contacts: " + sync.accountType);
if (sync.supportsUploading()) {
Log.d(TAG, "found SyncAdapter that supports contacts and is not read-only: " + sync.accountType);
// we'll now get a list of all accounts under that accountType:
Account[] accounts = AccountManager.get(this).getAccountsByType(sync.accountType);
for (Account account : accounts) {
Log.d(TAG, account.type + "/" + account.name);
}
}
}
}
Ne hésitez pas à explorer l'autre bonne chose dans SyncAdapterType
comme isUserVisible
que vous pourriez vouloir vérifier aussi bien.
Je peux obtenir tous les comptes de cette façon. Mais lequel puis-je utiliser pour enregistrer les contacts? Par exemple, j'ai 3 comptes (google, whatsapp, viber) mais en fait je peux enregistrer des contacts uniquement sur google account (également au Local Phone Storage et Sim Card). –
sûr, voir ma mise à jour – marmor
Merci beaucoup! –