In new Android version you can't get the accounts with code due to security reason:-
it needs to be prompt to user, and if user agreed then only can be proceed with it.The code will be looks like below :-
private val chooseAccount = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){ result: ActivityResult ->
result.apply {
if (resultCode == RESULT_OK) {
Timber.d("resultCode ==RESULT_OK")
Timber.d(data?.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE))
Timber.d(data?.getStringExtra(AccountManager.KEY_ACCOUNT_NAME))
} else if (resultCode == RESULT_CANCELED) {
Timber.d("resultCode ==RESULT_CANCELED")
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
//Note: setAllowableAccountsTypes you can change it as per your need
chooseAccount.launch(
AccountPicker.newChooseAccountIntent(
AccountPicker.AccountChooserOptions.Builder()
.setAllowableAccountsTypes(listOf(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE))
.setAlwaysShowAccountPicker(true)
.build()
))
}