我想检查一个特定的应用程序是否需要在运行时处理 Android Marshmallow 运行时权限。
下面的假设是否正确?
/**
* Checks whether runtime permissions must be handled or not.
*
* @param context Application context.
* @return Handle runtime permissions or not.
*/
public static boolean shouldCheckRuntimePermissions(Context context) {
return
isApplicationWithMarshmallowTargetSdkVersion(context) &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}
/**
* Checks whether the app is compiled with targetSdkVersion Marshmallow or above.
*
* @param context Application context.
* @return Application targetSdkVersion above 23 or not.
*/
public static boolean isApplicationWithMarshmallowTargetSdkVersion(Context context) {
return
context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.M;
}
这是我详细说明的表格,其中例如需要 ACCESS_COARSE_LOCATION
权限才能向具有 Android Marshmallow 运行时权限的用户询问。
====================
target SDK |<23| 23|
====================
|<23| X | X |
------------|
OS SDK |23 | X | V |
====================
我知道支持库的存在helpers ,但我想在这种特殊情况下避免使用它们。
最佳答案
@LuisMiguelSierra 是对的,我在问题末尾发布的链接中找到了答案,但我根本没有意识到。这是回答我的问题并证实我的假设和方法是正确的引述:
On all versions of Android, your app needs to declare both the normal and the dangerous permissions it needs in its app manifest, as described in Declaring Permissions. However, the effect of that declaration is different depending on the system version and your app's target SDK level:
- If the device is running Android 5.1 or lower, or your app's target SDK is 22 or lower: If you list a dangerous permission in your
manifest, the user has to grant the permission when they install the
app; if they do not grant the permission, the system does not install the app at all.- If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited
capabilities even if the user denies a permission request.
因此这个关于何时请求权限的假设得到证实:
====================
target SDK |<23| 23|
====================
|<23| X | X |
------------|
OS SDK |23 | X | V |
====================
关于Android:检查是否必须询问运行时权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37482314/