Android: 检查是否必须询问运行时权限
Android: Check whether runtime permissions must be asked or not
我想检查一个特定的应用程序是否需要在运行时处理 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;
}
这里是 table 我已经详细说明了在哪里,例如需要 ACCESS_COARSE_LOCATION
权限来询问具有 Android Marshmallow 运行时权限的用户。
====================
target SDK |<23| 23|
====================
|<23| X | X |
------------|
OS SDK |23 | X | V |
====================
我知道支持库 helpers 的存在,但我想在这种特殊情况下避免使用它们。
我 activity 检查是否(API 版本 >= 23 并且权限被拒绝)然后请求权限
if (android.os.Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_LOCATION_PERMISSION);
}
要从用户那里得到结果,只需重写
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_LOCATION_PERMISSION:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG,"Premission granted");
}else {
Log.d(TAG,"Premission denied");
}
break;
}
}
您可以尝试使用 Google 示例中的 EasyPermissions 库。它处理与权限相关的检查,您无需担心检查版本。
@LuisMiguelSierra 是对的,我在问题末尾发布的 link 中得到了答案,但我根本没有意识到。这是回答我的问题并证实我的假设和方法是正确的引述:
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 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;
}
这里是 table 我已经详细说明了在哪里,例如需要 ACCESS_COARSE_LOCATION
权限来询问具有 Android Marshmallow 运行时权限的用户。
====================
target SDK |<23| 23|
====================
|<23| X | X |
------------|
OS SDK |23 | X | V |
====================
我知道支持库 helpers 的存在,但我想在这种特殊情况下避免使用它们。
我 activity 检查是否(API 版本 >= 23 并且权限被拒绝)然后请求权限
if (android.os.Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_LOCATION_PERMISSION);
}
要从用户那里得到结果,只需重写
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_LOCATION_PERMISSION:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG,"Premission granted");
}else {
Log.d(TAG,"Premission denied");
}
break;
}
}
您可以尝试使用 Google 示例中的 EasyPermissions 库。它处理与权限相关的检查,您无需担心检查版本。
@LuisMiguelSierra 是对的,我在问题末尾发布的 link 中得到了答案,但我根本没有意识到。这是回答我的问题并证实我的假设和方法是正确的引述:
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 |
====================