头图

Author/Software Engineer Peter Visontay and Bessie Jiang

Contributor/Software Engineer Inara Ramji, Interaction Designer Rodrigo Farell, Product Manager James Kelly, Project Manager Henry Chin

Most users spend a lot of time on smartphones. Whether it's work, playing games, or connecting with friends, people always use apps as their main way of digital life. Apps usually need to request certain permissions in order to function properly, but when there are dozens of apps on any given device, it can be difficult to keep previously granted permissions up to date, especially if you haven’t used a certain permission for a long time. Application hours.

We have introduced the automatic permission reset function in Android 11. This feature helps protect user privacy: if the user has not used an app for several months, this feature will automatically reset the this app, which is the permission that is displayed to the user when requested. Starting in December 2021, we will expand this feature to billions of devices. This feature will be automatically enabled on devices running Android 6.0 (API level 23) or higher that use Google Play services .

The system will enable this feature by default for apps targeting Android 11 (API level 30) or higher. However, users can manually enable automatic permission reset for apps targeting API levels 23 to 29.

So, what does this mean for developers?

exception

Some applications and permissions will be automatically exempted from being revoked, such as the active device administrator application used by enterprises, and permissions fixed by corporate policies.

Request user to disable automatic reset

If necessary, the developer can request the user to prevent the system from resetting the permissions of their application. It is suitable for situations where the user expects the application to run mainly in the background without even interacting with it. You can view the main use case .

Compare current behavior with new behavior

Necessary code changes

If an application targets API 30 and higher and requests the user to disable the automatic reset of permissions, then the developer needs to make some simple code changes. If the app does not disable automatic reset, no code changes are required.

Note: This API is only applicable to applications whose targetSDK is API 30 or higher, because only these applications have permission to reset automatically. If the targetSDK of the application is API 29 or lower, the developer does not need to make any changes.

The following table summarizes the new cross-platform APIs (compared to the APIs released in Android 11

operateAndroid 11 API (for Android 11 and higher devices)New cross-platform API (applicable to devices with Android 6.0 and higher, including devices with Android 11 and higher)
Check if the device is enabled for automatic resetting of permissionsCheck if Build.VERSION.SDK_INT >= Build.VERSION_CODES.RCall androidx.core.content.PackageManagerCompat.getUnusedAppRestrictionsStatus()
Check if automatic reset is disabled for your appCall PackageManager.isAutoRevokeWhitelisted() )Call androidx.core.content.PackageManagerCompat.getUnusedAppRestrictionsStatus()
Ask the user to disable automatic resets for your appSend intent Intent with Send the intent created androidx.core.content.IntentCompat.createManageUnusedAppRestrictionsIntent()

This cross-platform API belongs to the Jetpack Core library and will be launched in Jetpack Core v1.7.0, and the Beta version has now been released.

A logic example that requires the user to disable the automatic deactivation of automatic reset:

val future: ListenableFuture<Int> =
    PackageManagerCompat.getUnusedAppRestrictionsStatus(context)
future.addListener(
  { onResult(future.get()) },
   ContextCompat.getMainExecutor(context)
)

fun onResult(appRestrictionsStatus: Int) {
  when (appRestrictionsStatus) {
    // Status could not be fetched. Check logs for details.
    ERROR -> { }

    // Restrictions do not apply to your app on this device.
    FEATURE_NOT_AVAILABLE -> { }
    // Restrictions have been disabled by the user for your app.
    DISABLED -> { }

    // If the user doesn't start your app 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(appRestrictionsStatus)
  }
}

fun handleRestrictions(appRestrictionsStatus: Int) {
  // If your app 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)
}

The above logic applies to Android 6.0 to Android 10, and Android 11 and higher devices. Just use the new API, you no longer need to call Android 11's automatic reset API.

is compatible with the application sleep function in Android 12

The new API is also compatible with Android 12 (API level 31). Hibernation is a new restriction that applies to unused apps. This feature does not apply to operating system versions prior to Android 12.

If both automatic permission reset and application sleep are applied to an application, getUnusedAppRestrictionsStatus() API will return to API_31 .

release schedule

You are welcome to continue to follow us and get the latest information at any time.

You are welcome to click here to submit feedback to us, or share your favorite content or problems found. Your feedback is very important to us, thank you for your support!


Android开发者
404 声望2k 粉丝

Android 最新开发技术更新,包括 Kotlin、Android Studio、Jetpack 和 Android 最新系统技术特性分享。更多内容,请关注 官方 Android 开发者文档。