日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

AccountManager getAccount 在Android O 8.0版本中获取为 null ?

發布時間:2023/12/15 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AccountManager getAccount 在Android O 8.0版本中获取为 null ? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題

AccountManager accountManager = AccountManager.get(this);Account[] accounts = accountManager.getAccounts();

以上代碼在 Android 8.0 (API 26) 之前運行地很好,能夠獲取到 account 信息。但是在最新版本 8.0 上卻獲取不到,返回 accounts 為 null.

另外,在 Why do I get null from retrieving the user’s gmail?
也是類似的問題。

原因

后來知道,這是 Android 8.0 的 行為變更。

Account access and discoverability
In Android 8.0 (API level 26), apps can no longer get access to user accounts unless the authenticator owns the accounts or the user grants that access. The GET_ACCOUNTS permission is no longer sufficient. To be granted access to an account, apps should either use AccountManager.newChooseAccountIntent() or an authenticator-specific method. After getting access to accounts, an app can can call AccountManager.getAccounts() to access them.
Android 8.0 deprecates LOGIN_ACCOUNTS_CHANGED_ACTION. Apps should instead use addOnAccountsUpdatedListener() to get updates about accounts during runtime.
For information about new APIs and methods added for account access and discoverability, see Account Access and Discoverability in the New APIs section of this document

除非身份驗證器擁有用戶帳號或用戶授予訪問權限,否則,應用將無法再訪問用戶帳號。僅擁有 GET_ACCOUNTS 權限尚不足以訪問用戶帳號。要獲得帳號訪問權限,應用應使用 AccountManager.newChooseAccountIntent() 或特定于身份驗證器的函數。獲得帳號訪問權限后,應用可以調用 AccountManager.getAccounts() 來訪問帳號。

Android 8.0 已棄用 LOGIN_ACCOUNTS_CHANGED_ACTION。相反,應用在運行時應使用 addOnAccountsUpdatedListener() 獲取帳號更新信息。

有關新增 API 和增加的帳號訪問和可檢測性函數的信息,請參閱此文檔的“新增 API”部分中的帳號訪問和可檢測性。

另外可參考,下面這篇文章
android 8.0 —AccountManager之行為變更

解決

根據文檔,

要獲得帳號訪問權限,應用應使用 AccountManager.newChooseAccountIntent() 或特定于身份驗證器的函數。獲得帳號訪問權限后,應用可以調用 AccountManager.getAccounts() 來訪問帳號。

于是

Intent googlePicker = AccountManager.newChooseAccountIntent(null, null,new String[] { "com.google"}, true, null, null, null, null); startActivityForResult(googlePicker, PICK_ACCOUNT_REQUEST); @Overrideprotected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {if (requestCode == PICK_ACCOUNT_REQUEST && resultCode == RESULT_OK) {String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);Log.d(TAG, "Account Name=" + accountName);String accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);Log.d(TAG, "Account type=" + accountType);AccountManager accountManager = AccountManager.get(this);Account[] accounts = accountManager.getAccounts();for (Account a : accounts) {Log.d(TAG, "type--- " + a.type + " ---- name---- " + a.name);}}}

問題可以得到解決。

總結

以上是生活随笔為你收集整理的AccountManager getAccount 在Android O 8.0版本中获取为 null ?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。