Android: Check whether runtime permissions must be asked or not

為{幸葍}努か 提交于 2019-12-10 18:14:22

问题


I would like to check whether a particular app need to handle Android Marshmallow runtime permissions in runtime or not.

Is it the following assumption correct?

/**
 * 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;
}

Here is the table I have elaborated where for example ACCESS_COARSE_LOCATION permissions were needed to ask to the user with Android Marshmallow runtime permissions.

====================
target SDK |<23| 23|
====================
       |<23| X | X |
       ------------| 
OS SDK |23 | X | V |
====================   

I know the existence of Support library helpers, but I want to avoid them for this particular case.


回答1:


@LuisMiguelSierra was right, I had the answer in the link I posted at the end of my question and I didn't realize at all. Here is the quote that answers my question and confirms my assumption and methods were right:

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.

So this assumption of when to ask permissions is confirmed:

====================
target SDK |<23| 23|
====================
       |<23| X | X |
       ------------| 
OS SDK |23 | X | V |
====================



回答2:


I your activity check if (the API version >= 23 and the permission is denied) then request the permission

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);
}

And to get the result from the user just override

 @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;
    }
}



回答3:


You can try using the EasyPermissions library from Google Samples. It handles the permission related checks and you don't need to worry about the checking the versions.



来源:https://stackoverflow.com/questions/37482314/android-check-whether-runtime-permissions-must-be-asked-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!