1

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

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

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() 条件的日志。并打印日志。但它不要求许可。

4

1 回答 1

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
    }
}
于 2016-09-30T08:04:18.863 回答