从设置中检查Android运行时权限

时间:2017-03-07 10:35:34

标签: android android-permissions runtime-permissions

我在Android应用中实现了Android运行时权限。我还能够检查在运行时是否允许或拒绝权限。但我想知道是否有用户首先拒绝权限然后通过设置中的应用权限菜单允许它。 (假设始终运行服务以检查是否发生任何更改)

2 个答案:

答案 0 :(得分:2)

I am also able to check if a permission is allowed or denied at runtime的一个小技巧。保存此详细信息(拒绝或允许),然后如果用户从设置中更改了某些内容,您可以轻松地比较应用的权限功能中的先前详细信息。

例如:您保存的详细信息被拒绝,然后您通过设置允许权限,如果您在权限功能中打开您的应用,您可以看到您已保存的详细信息被拒绝但现在您已获得许可,反之亦然。

答案 1 :(得分:0)

我使用以下帮助程序类来检查和请求权限

public final class Permissions {

public static final int INTERNAL_STORAGE_REQUEST_CODE = 1;
public static final int FINE_LOCATION_REQUEST_CODE = 2;
private static final int COARSE_LOCATION_REQUEST_CODE = 3;

public static final String READ_EXTERNAL_STORAGE = Manifest.permission.READ_EXTERNAL_STORAGE;
public static final String ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
public static final String ACCESS_COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;


public static boolean havePermissionFor(Context context, String permission) {
    return Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
            ActivityCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;

}


public static void showPermissionDetails(String message, Context context, DialogInterface.OnClickListener okListener) {
    M.showAlert(context, "Request for permission", message, "ALLOW", "DON'T ALLOW", okListener, null, false);
}

public static void requestForPermissions(Activity activity, String[] permissions, int permissionRequestCode) {
    ActivityCompat.requestPermissions(activity, permissions, permissionRequestCode);
}

public static void requestPermissionForFragment(Fragment fragment, String[] permissions, int permissionRequestCode) {
    fragment.requestPermissions(permissions, permissionRequestCode);
}

public static boolean shouldShowPermissionRationale(Activity activity, String permission) {
    return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
}


public ArrayList<String> getRequiredPermissions(Context context, String... permissions) {
    ArrayList<String> requiredPermissionsList = new ArrayList<>();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return requiredPermissionsList;
    for (String permission : permissions) {
        if (!havePermissionFor(context, permission))
            requiredPermissionsList.add(permission);
    }
    return requiredPermissionsList;

}
}