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
operate | Android 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 permissions | Check if Build.VERSION.SDK_INT >= Build.VERSION_CODES.R | Call androidx.core.content.PackageManagerCompat.getUnusedAppRestrictionsStatus() |
Check if automatic reset is disabled for your app | Call PackageManager.isAutoRevokeWhitelisted() ) | Call androidx.core.content.PackageManagerCompat.getUnusedAppRestrictionsStatus() |
Ask the user to disable automatic resets for your app | Send 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
- September 15, 2021 -The cross-platform automatic reset API has now entered the testing phase (Jetpack Core 1.7.0 Beta library), so developers can start using these APIs now. Even on devices that do not support automatic reset of permissions, it is safe to use these APIs (the API will return FEATURE_NOT_AVAILABLE on these devices).
- October 2021 -Cross-platform automatic reset API can be used as a stable API (Jetpack Core 1.7.0).
- December 2021 -The automatic permission reset feature will begin to be gradually promoted on devices that are supported by Google Play services and run between Android 6.0 and Android 10. On these devices, users can go to the automatic reset settings page to enable/disable automatic resets for specific applications. The system will automatically reset the permissions of unused apps a few weeks after the device is enabled.
- 2022 -The automatic permission reset function will cover all devices running Android 6.0 to Android 10.
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!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。