在片段android中没有调用onRequestPermissionsResult

时间:2016-09-07 13:13:31

标签: android android-fragments

我通过使用片段中的以下代码获取用户的运行时权限。

code

并覆盖

if (ContextCompat.checkSelfPermission(getActivity(),
                                    Manifest.permission.READ_EXTERNAL_STORAGE)
                                    != PackageManager.PERMISSION_GRANTED) {


                                ActivityCompat.requestPermissions(mActivity,
                                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                                        STORAGE_PERMISSION_CODE);


                            }

但覆盖方法未被称为

请给我提示,我必须覆盖片段

7 个答案:

答案 0 :(得分:1)

使用FragmentCompat代替ActivityCompat

答案 1 :(得分:0)

将此代码用于Fragment

 private void requestReadExternalStorageCameraPermission() {
    if (FragmentCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
        Toast.show(getActivity(), getString(R.string.toast_permission), Toast.ToastType.ALERT);
    } else {
        FragmentCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE_CAMERA);
    }
}

 @Override
public void onRequestPermissionsResult(int requestCode, String permissions[],
                                       int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE_GALLARY:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                captureImageInitialization(1);
            }
            break;
        case PERMISSION_REQUEST_CODE_CAMERA:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED && grantResults[2] == PackageManager.PERMISSION_GRANTED) {
                captureImageInitialization(0);
            }


            break;
        default:
            break;
    }
}

答案 2 :(得分:0)

记住,为了进一步,你必须在ACTIVITY中做到这一点而不是片段。你可以在片段中解决这个问题,但这是不好的方式

这是在活动中。我检查权限并保存

private static final int REQUEST_CODE_GET_ACCOUNTS = 101;
    private static final int REQUEST_AUDIO_PERMISSION = 102;

 @TargetApi(23)
public void checkAudioPermission() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        playerFragment.setupVisualizerFxAndUI();
        return;
    }
    if (this.checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager
            .PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO},
                REQUEST_AUDIO_PERMISSION);
    } else {
        playerFragment.setupVisualizerFxAndUI();
    }
}

@TargetApi(23)
public void checkGmailPermission() {
    if (isDeviceOnline()) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            showGmailRecFragment(true);
            return;
        }
        if (this.checkSelfPermission(Manifest.permission.GET_ACCOUNTS) != PackageManager
                .PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.GET_ACCOUNTS},
                    REQUEST_CODE_GET_ACCOUNTS);
            return;
        } else {
            showGmailRecFragment(true);
        }
    } else {
        Utils.showToast(this, getString(R.string.no_internet));
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
        grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_GET_ACCOUNTS:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                showGmailRecFragment(true);
            } else {
                Utils.showToast(this, getString(R.string.accounts_permision_denied));
            }
            break;
        case REQUEST_AUDIO_PERMISSION:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                playerFragment.setupVisualizerFxAndUI();
            } else {
                Utils.showToast(this, getString(R.string.audio_permission_denied));
            }
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            break;
    }
}

当我想要再次检查权限或用户拒绝它时,这是片段。这也保存了权限

private void setupViewVisualizer() {
    if (!isLiveTv && !homeVideo.isVideoType()) {
        ((PlayerActivity) activity).checkAudioPermission();
    } else {
        return;
    }
}

答案 3 :(得分:0)

试试这个..

 private static final int REQUEST_RUNTIME_PERMISSION = 123;

if (CheckPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)) {
// you have permission go ahead

} else {
// you do not have permission go request runtime permissions
RequestPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE, REQUEST_RUNTIME_PERMISSION);
}


  @Override
    public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {

        switch (permsRequestCode) {

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

                } else {
                    // you do not have permission show toast.
                }
                return;
            }
        }
    }
    public void RequestPermission(Activity thisActivity, String Permission, int Code) {
        if (ContextCompat.checkSelfPermission(thisActivity,
                Permission)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                    Permission)) {

            } else {
                ActivityCompat.requestPermissions(thisActivity,
                        new String[]{Permission},
                        Code);
            }
        }
    }

    public boolean CheckPermission(Activity context, String Permission) {
        if (ContextCompat.checkSelfPermission(context,
                Permission) == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

答案 4 :(得分:0)

我遇到了同样的问题,

这是一个常见的错误,很容易解决。

如果您正在参加活动,我们通常会使用 ActivityCompat.requestPermissions(activity, permissionsNeeded.toTypedArray(),PERMISSION_REQUEST_CODE)

但是您不应该对片段使用相同的内容。您可以像requestPermissions(permissionsNeeded.toTypedArray(),PERMISSION_REQUEST_CODE)

那样简单地使用它

如果您在片段中调用Activity.requestPermission,则会在Activity中而不是在片段中调用按需回调。

快乐编码

答案 5 :(得分:0)

从片段请求权限时

  • 科特琳

    requireActivity().requestPermissions(<Permission array>, <Request code>)

  • Java

    getActivity().requestPermissions(<Permission array>, <Request code>)

您可以从活动的onRequestPermissionsResult调用片段onRequestPermissionsResult

  • 科特琳
        var fragments:List? = supportFragmentManager.fragments
        var lastFragment:Fragment? = if(fragments!=null && fragments.size>0) fragments.get(fragments.size-1) else null
        var lastFragmentName:String? = if(lastFragment!=null) lastFragment.javaClass.name else ""
    
        if (lastFragmentName.equals("com.example.YourFragment")){
            lastFragment?.onRequestPermissionsResult(requestCode,permissions,grantResults)
        }
  • Java
        List fragments = getSupportFragmentManager().getFragments();
        Fragment lastFragment = (fragments!=null && fragments.size()>0)?fragments.get(fragments.size()-1) : null;
        String lastFragmentName = (lastFragment!=null) ? lastFragment.getClass().getName() : "";
if (lastFragment!=null && lastFragmentName.equals("com.example.YourFragment")){ lastFragment.onRequestPermissionsResult(requestCode,permissions,grantResults); }

答案 6 :(得分:0)

Fragment调用requestPermissions方法中处理权限。如果您在片段和包含该片段的活动中都覆盖onRequestPermissionsResult方法,请确保在活动方法中调用super.onRequestPermissionsResult(...),以将调用传播到片段中的onRequestPermissionsResult方法。