changes in 8.0
,帐户不能仅通过
GET_ACCOUNTS
许可。您还需要要求用户使用
AccountManager.newChooseAccountIntent
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if(checkSelfPermission(Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED) {
val intent = AccountManager.newChooseAccountIntent(null, null, arrayOf("com.google"), null, null, null, null)
startActivityForResult(intent, 42)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
if (requestCode == 42)
val accountManager = AccountManager.get(this)
val accounts = accountManager.getAccountsByType("com.google")
accounts.forEach {
Log.v(localClassName, it.toString())
}
}
}
如以下文件所述:
:
因此,您只需选择一次帐户,然后系统应记住此选项,以便后续调用
AccountManager.getAccountsByType