首页 > 业界资讯 > 正文

谷歌:12月起,数十亿台安卓手机等支持权限自动重置

  谷歌 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

  一个需要用户禁用自动停用自动重置的逻辑示例:

  val future: ListenableFuture =

  PackageManagerCompat.getUnusedAppRestrictionsStatus(context)

  future.addListener(

  { onResult(future.get()) },

  ContextCompat.getMainExecutor(context)

  )

  fun onResult(RestrictionsStatus: Int) {

  when (RestrictionsStatus) {

  // Status could not be fetched. Check logs for details.

  ERROR -> { }

  // Restrictions do not ly to your on this device.

  FEATURE_NOT_AVAILABLE -> { }

  // Restrictions have been disabled by the user for your .

  DISABLED -> { }

  // If the user doesn't start your for months, its permissions

  // will be revoked and/or it will be hibernated.

  // See the API_* constants for details.

  API_30_BACKPORT, API_30, API_31 ->

  handleRestrictions(RestrictionsStatus)

  }

  }

  fun handleRestrictions(RestrictionsStatus: Int) {

  // If your works primarily in the background, you can ask the user

  // to disable these restrictions. Check if you have already asked the

  // user to disable these restrictions. If not, you can show a message to

  // the user explaining why permission auto-reset and Hibernation should be

  // disabled. Tell them that they will now be redirected to a page where

  // they can disable these features.

  Intent intent = IntentCompat.createManageUnusedAppRestrictionsIntent

  (context, packageName)

  // Must use startActivityForResult(), not startActivity(), even if

  // you don't use the result code returned in onActivityResult().

  startActivityForResult(intent, REQUEST_CODE)

  }

  IT之家获悉,以上逻辑适用于 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 之间版本的设备。

  【来源:IT之家】【作者:潇公子】

网友评论

三日内热门评论文章
热门IT产品
  1. ¥7599
    苹果iPhoneX 64GB
    ·
  2. ¥5799
    三星S9
    ·
  3. ¥4498
    vivo NEX旗舰版
    ·
  4. ¥4999
    OPPO Find X
    ·
  5. ¥1799
    努比亚Z18mini
    ·
  6. ¥1499
    OPPO A5
    ·
  7. ¥1999
    荣耀Play(4GB RAM)
    ·
  8. ¥1598
    vivo Y85
    ·
  9. ¥3499
    坚果R1(6GB RAM)
    ·
  10. ¥3599
    一加6(8GB RAM)
    ·
为您推荐
  • 相关阅读
  • 业界资讯
  • 手机通讯
  • 电脑办公
  • 新奇数码
  • 软件游戏
  • 科学探索