我们可以在android中的不同位置询问相同的运行时权限吗?

时间:2016-09-30 07:43:03

标签: android android-studio runtime-permissions

我有一个导航抽屉。在导航抽屉中,我正在对片段进行充气。在第一个片段中,我向用户询问运行时权限。但在第二个片段中,我想这样做,它再次要求用户获得相同的权限。但在我的情况下,第一个片段请求许可。但在第二个片段中,它不会要求许可。所以有人能告诉我这是什么问题吗?因为我在这两种情况下都使用相同的代码。

这是我的第一个片段代码: -

if (checkPermission(Manifest.permission.ACCESS_FINE_LOCATION, getActivity().getApplicationContext(), getActivity())) {

                    IsConsumed = 1;
                    fetchLocationData();
                } else {

                    requestPermission(Manifest.permission.ACCESS_FINE_LOCATION, PERMISSION_REQUEST_CODE_LOCATION, getActivity().getApplicationContext(), getActivity());
                }


 public void requestPermission(String strPermission, int perCode, Context _c, Activity _a) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(_a, strPermission)) {


            Toast.makeText(getActivity(), "GPS permission allows us to access location data. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        } else {

            ActivityCompat.requestPermissions(_a, new String[]{strPermission}, perCode);
        }
    }


    public static boolean checkPermission(String strPermission, Context _c, Activity _a) {
        int result = ContextCompat.checkSelfPermission(_c, strPermission);
        if (result == PackageManager.PERMISSION_GRANTED) {

            return true;

        } else {

            return false;

        }
    }
 @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {

            case PERMISSION_REQUEST_CODE_LOCATION:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    fetchLocationData();

                } else {

                    Toast.makeText(getActivity(), "Permission Denied, You cannot access location data.", Toast.LENGTH_LONG).show();

                }
                break;

        }
    }

我在第二个片段中使用相同的代码。但它没有要求权限。我试图打印它进入if()条件的日志。并打印日志。但它没有要求许可。

1 个答案:

答案 0 :(得分:0)

public static boolean checkPermission(String strPermission, Context _c, Activity _a) {
    int result = ContextCompat.checkSelfPermission(_c, strPermission);
    if (result == PackageManager.PERMISSION_GRANTED) {

        return true;

    } else {
        // user allowed permission. we should not ask for permission.    
        return false;

    }
}

这是检查用户是否已授予权限的功能。

在第一个片段中,如果用户允许此权限,那么 else部分中的代码将会运行。

您可以在else块中调用requestPermission函数再次请求权限(不建议再次请求相同的权限)

public static boolean checkPermission(String strPermission, Context _c, Activity _a) {
    int result = ContextCompat.checkSelfPermission(_c, strPermission);
    if (result == PackageManager.PERMISSION_GRANTED) {

        return true;

    } else {
        requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                            1);
        return false;

    }
}

然后调用以下函数来处理允许和拒绝点击。

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                //Called when Allowed
            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                //Called when denied
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}