1. 程式人生 > 資訊 >谷歌:12 月起,數十億檯安卓手機等支援許可權自動重置,包括 Android 6.0~11

谷歌:12 月起,數十億檯安卓手機等支援許可權自動重置,包括 Android 6.0~11

10 月 20 日訊息,谷歌 Android 開發者部落格今天發文稱,為數十億臺裝置提供許可權自動重置功能。

應用通常需要請求某些許可權才能正常執行,但在任何給定的裝置都有數十個應用的情況下,要讓之前授予的許可權保持最新狀態可能很困難,特別是在你長時間未使用某個應用時。

谷歌稱,在 Android 11 中引入了許可權自動重置功能。這項功能有助於保護使用者的隱私: 如果使用者幾個月未使用某應用,該功能就會自動重置此應用的執行時許可權,即請求時向用戶顯示提示的許可權。2021 年 12 月起,谷歌會將這項功能擴充套件到數十億臺裝置。該功能將自動在執行 Android 6.0 (API 級別 23) 或更高版本的使用 Google Play 服務的裝置上啟用。

許可權自動重置功能

https://developer.android.google.cn/about/versions/11/privacy/permissions#auto-reset

執行時許可權

https://developer.android.google.cn/guide/topics/permissions/overview#runtime

Google Play 服務

https://developers.google.cn/android

系統將預設為面向 Android 11 (API 級別 30) 或更高版本的應用啟用該功能。不過,使用者可以為面向 API 級別 23 到 29 的應用手動啟用許可權自動重置功能。

那麼,這對開發者來說意味著什麼呢?

例外

一些應用和許可權將自動免於撤消,如企業使用的活動裝置管理員應用,以及由企業政策固定的許可權。

請求使用者停用自動重置

如有需要,開發者可以請求使用者阻止系統重置其應用的許可權。適用於使用者期望應用主要在後臺執行,甚至無需與其互動的情況。您可以檢視主要用例:

https://developer.android.google.cn/training/permissions/requesting#request-disable-auto-reset

比較當前行為與新行為

必要的程式碼更改

如果一個應用面向 API 30 及更高版本,並請求使用者停用許可權自動重置,那麼開發者需要做一些簡單的程式碼更改。如果應用不停用自動重置,則無需進行程式碼更改。

注:此 API 僅適用於 targetSDK 為 API 30 或更高版本的應用,因為僅這些應用具有許可權自動重置。如果應用的 targetSDK 為 API 29 或更低版本,則開發者無需進行任何更改。

下表彙總了新的跨平臺 API (與 Android 11 中釋出的 API 相比):

Android 11

https://developer.android.google.cn/training/permissions/requesting#auto-reset-permissions-unused-apps

PackageManager.isAutoRevokeWhitelisted()

https://developer.android.google.cn/reference/android/content/pm/PackageManager#isAutoRevokeWhitelisted(java.lang.String)

Intent.ACTION_AUTO_REVOKE_PERMISSIONS

https://developer.android.google.cn/reference/android/content/Intent#ACTION_AUTO_REVOKE_PERMISSIONS

這個跨平臺 API 屬於 Jetpack Core 庫,將於 Jetpack Core v1.7.0 中推出,現已釋出 Beta 版:

https://developer.android.google.cn/jetpack/androidx/releases/core

一個需要使用者禁用自動停用自動重置的邏輯示例:

valfuture:ListenableFuture<Int>=PackageManagerCompat.getUnusedAppRestrictionsStatus(context)future.addListener({onResult(future.get())},ContextCompat.getMainExecutor(context))funonResult(RestrictionsStatus:Int){when(RestrictionsStatus){//Statuscouldnotbefetched.Checklogsfordetails.ERROR->{}//Restrictionsdonotlytoyouronthisdevice.FEATURE_NOT_AVAILABLE->{}//Restrictionshavebeendisabledbytheuserforyour.DISABLED->{}//Iftheuserdoesn'tstartyourformonths,itspermissions//willberevokedand/oritwillbehibernated.//SeetheAPI_*constantsfordetails.API_30_BACKPORT,API_30,API_31->handleRestrictions(RestrictionsStatus)}}funhandleRestrictions(RestrictionsStatus:Int){//Ifyourworksprimarilyinthebackground,youcanasktheuser//todisabletheserestrictions.Checkifyouhavealreadyaskedthe//usertodisabletheserestrictions.Ifnot,youcanshowamessageto//theuserexplainingwhypermissionauto-resetandHibernationshouldbe//disabled.Tellthemthattheywillnowberedirectedtoapagewhere//theycandisablethesefeatures.Intentintent=IntentCompat.createManageUnusedAppRestrictionsIntent(context,packageName)//MustusestartActivityForResult(),notstartActivity(),evenif//youdon'tusetheresultcodereturnedinonActivityResult().startActivityForResult(intent,REQUEST_CODE)}

獲悉,以上邏輯適用於 Android 6.0 到 Android 10,以及 Android 11 和更高版本的裝置。只需使用新 API 即可,您無需再呼叫 Android 11 的自動重置 API。

與 Android 12 中應用休眠功能的相容

新 API 同樣與 Android 12 (API 級別 31) 中引入的應用休眠功能相容。休眠是適用於未使用應用的一種新限制。該功能不適用於 Android 12 之前的作業系統版本。

Android 12 (API 級別 31) 中引入的應用休眠功能

https://developer.android.google.cn/about/versions/12/behavior-changes-all

如果許可權自動重置和應用休眠都應用於一個應用,則 getUnusedAppRestrictionsStatus () API 將返回 API_31。

釋出時間表

2021 年 9 月 15 日 - 跨平臺自動重置 API 現已進入測試階段 (Jetpack Core 1.7.0 Beta 版庫),所以開發者現在就可以開始使用這些 API。即使在不支援許可權自動重置的裝置上,使用這些 API 也是安全的 (API 在這些裝置上會返回 FEATURE_NOT_AVAILABLE)。

2021 年 10 月 - 跨平臺自動重置 API 可作為穩定的 API 使用 (Jetpack Core 1.7.0)。

2021 年 12 月 - 許可權自動重置功能將開始在由 Google Play 服務提供支援並執行 Android 6.0 到 Android 10 之間版本的裝置上逐步推廣。在這些裝置上,使用者可以前往自動重置設定頁面,針對特定應用啟用/停用自動重置。系統將在裝置啟用該功能幾周後開始自動重置未使用應用的許可權。

2022 年第 1 季度 - 許可權自動重置功能將覆蓋所有執行 Android 6.0 到 Android 10 之間版本的裝置。